Ok this (truncated query) runs fine. Got some expert advice a month ago to fix it.
SELECT * FROM artWork WHERE art_id in (
SELECT art_id FROM artWork AS a INNER JOIN userPrefs AS u ON ((
((u.media_oil='1' AND a.media_oil='1') OR
(u.media_acrylic='1' AND a.media_acrylic='1') OR
(u.media_wc='1' AND a.media_wc='1') OR
(u.media_pastel='1' AND a.media_pastel='1'))
etc, etc........................................
WHERE a.artist_id NOT EXISTS (
SELECT * FROM removeList AS r
WHERE r.artist_id = a.artist_id
AND r.user_id ='$user_id')
AND a.make_avail='1'
AND a.cur_select='1'
AND u.user_id='$user_id'
AND ((u.pref_painting='1' AND a.pref_painting='1') OR
(u.pref_photo='1' AND a.pref_photo='1') OR
(u.pref_paper='1' AND a.pref_paper='1') OR
(u.pref_print='1' AND a.pref_print='1') OR
(u.pref_draw='1' AND a.pref_draw='1') OR
(u.pref_sculp='1' AND a.pref_sculp='1') OR
(u.pref_install='1' AND a.pref_install='1') OR
(u.pref_vid='1' AND a.pref_vid='1') OR
(u.pref_public='1' AND a.pref_public='1') OR
(u.pref_indef='1' AND a.pref_indef='1'))
) ORDER BY date_submit DESC
But now I need to exclude certain rows that may be in another 2 column (user_id & artist_id) child table: ‘removeList’. So I am trying without success to what amounts to a “triple join” (look for the code around ‘NOT EXISTS’):
SELECT * FROM artWork WHERE art_id in (
SELECT art_id FROM artWork AS a INNER JOIN userPrefs AS u ON (
(((u.media_oil='1' AND a.media_oil='1') OR
(u.media_acrylic='1' AND a.media_acrylic='1') OR
(u.media_wc='1' AND a.media_wc='1') OR
(u.media_pastel='1' AND a.media_pastel='1'))
etc, etc........................................
WHERE a.artist_id NOT EXISTS (
SELECT * FROM removeList AS r
WHERE r.artist_id = a.artist_id AND r.user_id ='$user_id'
)
AND a.make_avail='1'
AND a.cur_select='1'
AND u.user_id='$user_id'
AND (( u.pref_painting='1' AND a.pref_painting='1') OR
( u.pref_photo='1' AND a.pref_photo='1') OR
( u.pref_paper='1' AND a.pref_paper='1') OR
( u.pref_print='1' AND a.pref_print='1') OR
( u.pref_draw='1' AND a.pref_draw='1') OR
( u.pref_sculp='1' AND a.pref_sculp='1') OR
( u.pref_install='1' AND a.pref_install='1') OR
( u.pref_vid='1' AND a.pref_vid='1') OR
( u.pref_public='1' AND a.pref_public='1') OR
( u.pref_indef='1' AND a.pref_indef='1') )
) ORDER BY date_submit DESC
Am I reaching too far here? Is there a better approach I am overlooking. Thanks to all.
NOT EXISTS shouldn’t have a column name before it. remove the a.artist_id from before the not exists.
So the relevant line would say
Not sure where you’re using this query, but its also recommended to parameterize your queries to prevent sql injections.
Hope this helps!