当前位置: 动力学知识库 > 问答 > 编程问答 >

php - Mysql - select multiple columns with same value and other colums equal to zero

问题描述:

I have a mysql query that selects the records where col1 and col2 = 4 and col3 or col4 = 0

SELECT * FROM table WHERE 4 IN(col1, col2) && (col3 = 0 || col4 = 0)

The or is not working. It returns zero rows and I know that there are 4

网友答案:

&& and || in SQL are AND and OR.

Rewrite that to:

SELECT * FROM table WHERE (col1 = 4 AND col2 = 4) AND (col3 = 0 OR col4 = 0)

You can't use IN for column names. You can use IN like this: WHERE col3 IN('4','a','another valid value'), which is equals to WHERE (col3 = '4' OR col3 = 'a' OR ...)

网友答案:

Try like below instead cause per your posted code 4 IN (col1, col2) will be col1=4 or col2 = 4 whereas you want it to be and condition

Per last comment update; below query will get you exactly those records

There is a total of 4 rows where column1 or column2 = 4. of those rows I only want to return the rows where col3 or col4 = zero

SELECT * 
FROM test4 
WHERE (col1 = 4 or col2 = 4) 
and (col3 = 0 or col4 = 0)
分享给朋友:
您可能感兴趣的文章:
随机阅读: