从 mysql 中选择记录的位置


Selecting records from mysql where

如果您查看下面的 SQL,我无法整理查询(让我发疯),请注意 and/or。我需要基本上将 2 个类别中的艺术家图像合并到一个调用中,但是如果我使用以下 sql,我会获得第一类中的所有图像以及来自第二类别中每个艺术家的所有图像,而不是特定艺术家的图像。如果使用 AND 调用,则只会获取两者中的图像。所以基本上希望这是有道理的。感谢所有帮助。

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

如果您同时使用这两种AND/OR那么您需要使用括号来确定顺序

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

甚至使用以下WHERE子句:

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