Results 1 to 2 of 2

Thread: Multiple on expressions in a select?

  1. #1
    Join Date
    Nov 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Multiple on expressions in a select?

    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

  2. #2
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    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:

    Code:
    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

    Code:
    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
    Last edited by james438; 11-14-2009 at 09:52 PM.
    To choose the lesser of two evils is still to choose evil. My personal site

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •