Log in

View Full Version : Multiple on expressions in a select?



StLouieMIke
11-14-2009, 01:08 PM
I'm kinda new at this and I am getting into some more complex issues and I could use a hand, I have a select query that works ok now but I need to add more detail to it.
current query:

$query = "SELECT * FROM mess WHERE
toid='$fsnid' OR toid='9223372036854775807' AND status='a' ORDER BY toid ASC LIMIT 30";

mess is a table I am using to stream messages to users on my site I have another table called friends which contains 2 fields fsnid and friendsid I would like to be able to further limit the above query to only allow non friends (friendid=null) messages to be displayed when mess.messtype='c'.

I was thinking query below would do it but no such luck:

$query ="SELECT fromname,datetime,message,fromid
FROM mess
LEFT JOIN friends
ON mess.toid=$fsnid OR mess.toid='9223372036854775807'
and mess.status='a'
WHERE friends.friendsid=NULL AND mess.messtype='c'
ORDER BY toid ASC LIMIT 30";
Thanks
MC

james438
11-14-2009, 06:51 PM
It looks like you are selecting fromname, datetime, message, fromid from the table mess. By guessing as to which columns belong to which tables I would say you are looking for something along the lines of:


SELECT mess.fromname,mess.datetime,mess.message,mess.fromid
FROM mess,friends WHERE mess.toid=$fsnid OR mess.toid='9223372036854775807'
AND mess.status='a' AND friends.friendsid=NULL AND mess.messtype='c'
ORDER BY mess.toid ASC LIMIT 30

you could also write this as


SELECT mess.fromname,mess.datetime,mess.message,mess.fromid
FROM friends
LEFT JOIN mess
ON mess.toid=$fsnid OR mess.toid='9223372036854775807'
AND mess.status='a'
AND friends.friendsid=NULL
AND mess.messtype='c'
ORDER BY toid ASC LIMIT 30