-1

I wanted to know how to make MySQL count specific rows where a certain condition is true (All rows where the value for foo = bar) and return this value as a variable like:

SELECT COUNT(*) FROM test WHERE foo LIKE '".bar."'

and make this return e.g. 20 because there are 20 rows that contain bar

lurker
  • 56,987
  • 9
  • 69
  • 103
  • 1
    This SQL looks correct – meyer9 Mar 23 '17 at 16:07
  • 3
    What's wrong with what you suggested, other than perhaps the `LIKE` string, which maybe you meant, `'%.bar.%'`? Did you try it? If you really meant strictly `foo = bar` then it should read `WHERE foo = 'bar'`. – lurker Mar 23 '17 at 16:08
  • should be `foo=bar` for an exact match, `LIKE '%bar'` for strings that end with bar, `LIKE 'bar%'` for strings that begins with bar or `LIKE'%bar%'` for strings containing bar – Lelio Faieta Mar 23 '17 at 16:10
  • It seems like this SQL appears in a string literal within a programming language. If so, can you provide the complete statement and tag the question with the programming language? – trincot Mar 23 '17 at 16:12
  • Well it is meant to look for values LIKE '".$bar."' because upper and lowercase might vary and then count this amount – user407022 Mar 23 '17 at 16:14
  • Is this really a question about your particular computer programming language's access to the SQL? You haven't really asked a question. You've only made an assertion and some comments. – lurker Mar 23 '17 at 17:03
  • This question is a Read The Manual question and shows no effort to research and self-solve. This question is so basic that it will not serve SO readers in the future. Please delete this question. – mickmackusa Mar 24 '17 at 08:07
  • Possible duplicate of [MySQL different counts between "where =" and "where like"](http://stackoverflow.com/questions/37263964/mysql-different-counts-between-where-and-where-like) – mickmackusa Mar 24 '17 at 08:21
  • It was actually difficult to select a duplicate to flag this question, because there were hundreds of different duplicates that ask&answer this question. user407022, please research and attempt to self-solve before posting on SO. Look at how many people wasted their time telling you something you could have easily discovered on your own if you tried. – mickmackusa Mar 24 '17 at 08:25

2 Answers2

0

You need to remove " and .. Try below code

SELECT COUNT(*) FROM test WHERE foo LIKE 'bar'
Sachin PATIL
  • 745
  • 1
  • 9
  • 16
0

All rows where the value for foo = bar

select count(*) from test WHERE foo LIKE 'bar';

All rows where the value for foo start with bar

select count(*) from test WHERE foo LIKE 'bar%';

All rows where the value for foo end with bar

select count(*) from test WHERE foo LIKE '%bar';
benhenda89
  • 11
  • 2