开发者

How to select in mysql with 2 times WHERE

开发者 https://www.devze.com 2023-02-22 15:07 出处:网络
I have a table with destionations PRN, SKP, MUC etc. I want to select 15 rows with the destionation PRN and SKP, but I am not sure how to do this because when I write down

I have a table with destionations PRN, SKP, MUC etc.

I want to select 15 rows with the destionation PRN and SKP, but I am not sure how to do this because when I write down

SELECT * FROM destinations WHERE date >= NOW() AND destination = 'PRN' OR 'SKP'

it wont work it will show only where SKP or only where PRN, I have tried this with OR, AND adn so on.. but I cant a way that works and selects 15 last destinations where destiona开发者_开发知识库tion = PRN and SKP.


you can use as :

SELECT * FROM destinations WHERE date >= NOW() AND (destination = 'PRN' OR destination = 'SKP')

or use IN operator

SELECT * FROM destinations WHERE date >= NOW() AND destination IN ('PRN','SKP')

and for only 15 row you can add " LIMIT 15" after query


SELECT * FROM destinations WHERE date >= NOW() 
AND (destination = 'PRN' OR destination='SKP')
ORDER BY date desc LIMIT 15


SELECT * FROM destinations WHERE date >= NOW() AND destination IN ('PRN', 'SPK')

or

SELECT * FROM destinations WHERE date >= NOW() AND (destination = 'PRN' OR destination =  'SPK')

should do the trick


Using in is generally the most readable form (especially if you have multiple destinations):

SELECT * FROM destinations WHERE date >= NOW() AND destination IN ( 'PRN' , 'SKP' )

When using or, be sure to put brackets around the second term so its clear which operator is applied first:

SELECT * FROM destinations WHERE date >= NOW() AND (destination = 'PRN' OR destination = 'SPK')


The correct syntax is:

SELECT * FROM destinations WHERE date >= NOW() AND (destination = 'PRN' OR destination = 'SKP')

Or:

SELECT * FROM destinations WHERE date >= NOW() AND destination IN ('PRN', 'SKP')


SELECT * FROM destinations WHERE date >= NOW() AND destination IN ('PRN' , 'SKP')
0

精彩评论

暂无评论...
验证码 换一张
取 消