I need to do a PDO statement that cross-references 3 tables and it seems i’m stuck.
It basically needs 4 records from 2 tables (myl_contacts and mya_users) and they don’t need to be listed in myl_blocked_contacts.
In myl_blocked_contacts are the rows with id, and contact_type (‘a’, ‘l’, ‘x’, etc) which are blocked, and I need to select from myl_contacts users that I have and are NOT blocked (hence are not in myl_blocked contacts).
Here’s the piece of PDO::mysql query that I’ve done so far. I don’t know where to go from here, I am trying a lot of things off the net, but I seem to go in the wrong direction somehow. Thanks!
$query = "SELECT myl_contacts.contact_id, mya_users.name, mya_users.city, mya_users.ext
from myl_contacts, mya_users,
LEFT JOIN myl_blocked_contacts
WHERE myl_contacts.contact_type='a'
AND myl_contacts.label_id=:id
AND (myl_blocked_contacts.contact_type!='a'
AND myl_blocked_contacts.contact_id!=mya_users.id)
";
$result = $db->prepare($query);
$result->bindValue(':id', $_id, PDO::PARAM_INT);
$result->execute();
I’m not sure if I get your problem right.
I don’t understand why you use the
LEFT JOIN.Isn’t there a
ONpart needed? (like... t1 LEFT JOIN t2 ON t1.ID = t2.t1ID ...)What if you just write:
I also don’t quite understand why you
SELECTrows from different tables which are not related. This might make sense, but does not necessarily. If there’s some connection between these tables, you might want to add anINNER JOIN…