简体   繁体   中英

Selecting records from mysql where

I am having trouble sorting out a query (making me nuts) if you look at the SQL below, notice the and/or. I need to essentially combine the images for the artist in 2 categories into one call BUT if I use the below sql I get all of the images in the first category as well as all the images from every artist in the second category rather than the ones for the particular artist. If you use an AND call it only gets images in both. So basically hopefully this made sense. All help is appreciated.

SELECT i.img_name, i.ordered_by, i.img_description, a.artist_path_name, a.artist_id, a.artist_dir, a.artist_name, a.first_name, a.last_name, a.artist_titletag, a.artist_metadesc, a.artist_metakey, ck.catKey_id, ck.keyword
        FROM images AS i JOIN artists AS a USING (artist_id)
        JOIN img_cat_table AS imc USING (img_id)
        JOIN catKeys AS ck USING (catKey_id)
        WHERE artist_name = 'j' AND catKey_id = 5 OR catKey_id = 1

if you are using both AND/OR then you will want to use parentheses to determine the order

WHERE (artist_name = 'j') AND (catKey_id = 5 OR catKey_id = 1)

Or even use the following WHERE clause:

WHERE (artist_name = 'j') AND (catKey_id IN (5, 1))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM