What boolean operator do I want to use in this mysql statement?

What is the best way to select ans and quest from the table?

SELECT * FROM tablename WHERE option='ans' OR option='quest'";

      

OR

SELECT * FROM tablename WHERE option='ans' AND option='quest'";

      

Many thanks!

+2


a source to share


5 answers


Your second statement will not return any results. The entry cannot contain = ans and option = quest at the same time.



+7


a source


It is not a question of the "best" way - only the first one works. Even though you want option = ans and option = quest in your result set, the WHERE clause is executed once per line. So you tell MySQL "give me a line where option = quest and option = ans" ie option is two values ​​at the same time, which is impossible. You really want to get strings where either is true, which is why you are using OR

.

I think this reads better:



SELECT * FROM tablename WHERE option IN('ans','quest');

      

+4


a source


If the line is the question, it has option

a value 'quest'

, and a string with a response have option

set in 'ans'

, then you should use option='ans' OR option='quest'";

. Also a string cannot represent both a question and an answer, so using AND

will not select rows.

+2


a source


This selection will return all whos options lines, ans

orquest

SELECT * FROM tablename WHERE option='ans' OR option='quest'";

      

This selection, on the other hand, will not return rows as the column only has one of these values

SELECT * FROM tablename WHERE option='ans' AND option='quest'";

      

+1


a source


Use this if you want your search to return both answers and questions:

SELECT * FROM tablename WHERE option='ans' OR option='quest';

      

It can also be written:

SELECT * FROM tablename WHERE option in ('ans','quest');

      

0


a source







All Articles