Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sql - comparing substrings instead of entire string

select substring(datapath,1,5), COUNT(substring(datapath,1,5)) from batchinfo
where datapath like '%thc%'
group by datapath
having COUNT(substring(datapath,1,5))>1

i am trying to count how many of each substrings there are in the table and for some reason it counts the ENTIRE string. what am i doing wrong?

like image 217
JOE SKEET Avatar asked Jul 24 '26 11:07

JOE SKEET


1 Answers

You simply need to GROUP BY the substring you're trying to count instead of the full datapath. There's also no need to repeat the substring function on the count.

select substring(datapath,1,5), COUNT(*) 
    from batchinfo
    where datapath like '%thc%'
    group by substring(datapath,1,5)
    having COUNT(*)>1
like image 123
Joe Stefanelli Avatar answered Jul 26 '26 01:07

Joe Stefanelli



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!