Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
245 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

&& 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 ...)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...