IF Statement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • arggg
    New Member
    • Mar 2008
    • 91

    IF Statement

    What would be the correct format for this? I would like to see if tid IS NULL and if so leave out the last WHERE statement and not get anything from tsms table.

    [code=mysql] $sql = "SELECT * FROM users INNER JOIN tsms WHERE id='".$_REQUEST[2]."' ".
    "IF(tid NOT NULL) THEN AND tsms.uid=tid END IF";[/code]
  • write2ashokkumar
    New Member
    • Feb 2007
    • 39

    #2
    Originally posted by arggg
    What would be the correct format for this? I would like to see if tid IS NULL and if so leave out the last WHERE statement and not get anything from tsms table.
    [code=mysql] $sql = "SELECT * FROM users INNER JOIN tsms WHERE id='".$_REQUEST[2]."' ".
    "IF(tid NOT NULL) THEN AND tsms.uid=tid END IF";[/code]

    Hi,

    i think, u r trying to join the 2 table to get the data from the tables when the user id is available in both tables. But the above join syntax is wrong. Check with the following query, it will help for ur requirement.
    [code=mysql]
    SELECT usr.*
    FROM users usr
    INNER JOIN tsms ts ON usr.id = ts.uid
    WHERE usr.id = <$_REQUEST[2]>;[/code]

    Before try with the table join, you must learn the syntax of the table join.

    check the above link

    Regards,
    S.Ashokkumar

    Please enclose your posted code in [code] tags (See How to Ask a Question).

    This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

    Please use [code] tags in future.

    MODERATOR
    Last edited by ronverdonk; Mar 28 '08, 05:02 PM. Reason: code tags

    Comment

    • arggg
      New Member
      • Mar 2008
      • 91

      #3
      What I am wanting is to only pull data from the `tsms` table only if `tid` in the `users` table is not NULL.

      Comment

      • arggg
        New Member
        • Mar 2008
        • 91

        #4
        I believe I found the solution. It seems to work but if you can confirm this.

        [code=mysql]
        SELECT * FROM users LEFT JOIN tsms ON users.tid=tsms. uid[/code] This still retrieves the data from users like i want even if they are not in the tsms table.

        Comment

        Working...