Help with simple MYSQL Query

Discussion in 'MySQL' started by MattC1983, May 18, 2008.

  1. #1
    Hi i'm getting a syntax error for a query i am trying to run directly onto the database (so this is not in my php code). The query i am running is:

    SELECT * FROM table1 WHERE fkid1 NOT IN
    (
    SELECT pkid FROM table2
    )

    fkid1 and pkid are both fields in the the respective tables of exactly the same type

    I haven't had errors running this type of query on any other types of databases (oracle, sql server etc) and i can run it fine on another php/mysql server. It also errors if I use IN rather than NOT IN but does work if i replace the nested select statement with hard coded values.

    Is it possible that a version of mysql does not allow this? Anyone got any ideas on what might be causing the problem.

    Oh and the error is:

    #1064 - You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'select pkid from table2 ) LIMIT 0, 30' at line 1
     
    MattC1983, May 18, 2008 IP
  2. mwasif

    mwasif Active Member

    Messages:
    816
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    70
    #2
    The query you mentioned does not contain LIMIT but the error statement does...
     
    mwasif, May 18, 2008 IP
  3. allaboutgeo

    allaboutgeo Peon

    Messages:
    85
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Which mysql version you are using? And what do you mean by hard coded values?
     
    allaboutgeo, May 18, 2008 IP
  4. MattC1983

    MattC1983 Peon

    Messages:
    71
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    hi yeah sorry i just took the limit stuff off the query because that is what phpmyadmin put on automatically.

    When i say hardcoded i mean for example:

    select * from table1 where fk1 NOT IN (1,2,3,4,5)

    Thanks
     
    MattC1983, May 18, 2008 IP
  5. MattC1983

    MattC1983 Peon

    Messages:
    71
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Sorry forgot to mention, mysql version is: 5.0.32
     
    MattC1983, May 18, 2008 IP
  6. mwasif

    mwasif Active Member

    Messages:
    816
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    70
    #6
    The query looks fine. Try LEFT JOIN instead
    SELECT t1.* FROM table1 t1 
    LEFT JOIN table2 t2 ON t1.fkid1 = t2.pkid
    WHERE t1.fkid IS NULL
    Code (mysql):
     
    mwasif, May 18, 2008 IP
  7. MattC1983

    MattC1983 Peon

    Messages:
    71
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    ah didnt think of that, yeah that works fine thanks. Strange though.
     
    MattC1983, May 18, 2008 IP
  8. mwasif

    mwasif Active Member

    Messages:
    816
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    70
    #8
    Can you make a simple SELECT to if this is returning valid results
    SELECT pkid FROM table2
    Code (markup):
     
    mwasif, May 18, 2008 IP
  9. MattC1983

    MattC1983 Peon

    Messages:
    71
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    yeah i can, both selects work and return results. :\
     
    MattC1983, May 18, 2008 IP