SQL syntax issue

Discussion in 'MySQL' started by emitind, Feb 18, 2009.

  1. #1
    This query below has worked fine on most servers I've used, but one is throwing up an error. Doesn't seem to like the syntax. Is there a quick alternative to acheive the same result?

    	
    $qry="select * from rate_vars a where var_id in
    (select b.var_id from topic_ratevars b where topic_id=$topic_id ) ORDER BY var_name";
    
    PHP:
    Query error: 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 b.var_id from topic_ratevars b where topic_id=22 ) ORDER
    Code (markup):
    Thanks for any help in advance.
     
    emitind, Feb 18, 2009 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    First off, what's the reason for using the a and the b in this query? They both look unnecessary to me in this example.

    I would try this:

    SELECT * FROM rate_vars WHERE var_id IN
    (SELECT var_id FROM topic_ratevars WHERE topic_id = '$topic_id')
    ORDER BY var_name

    Also, an inner join may be a quicker way to perform this query, but the above should work.
     
    jestep, Feb 18, 2009 IP
  3. gnp

    gnp Peon

    Messages:
    137
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #3
    as mentioned by jestep, a join would fit better here..

    
    SELECT * FROM rate_vars rv INNER JOIN topic_ratevars trv ON rv.var_id = trv.var_id WHERE trv.topic_id = '$topic_id' ORDER BY rv.var_name
    
    Code (sql):
    hope this helps
     
    gnp, Feb 18, 2009 IP
  4. mwasif

    mwasif Active Member

    Messages:
    816
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    70
    #4
    Subqueries are not supported in MySQL versions less than 4.1.
     
    mwasif, Feb 18, 2009 IP
  5. NightMare49

    NightMare49 Member

    Messages:
    109
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #5
    try out this query

    $qry="select * from rate_vars a where a.var_id in
    (select b.var_id from topic_ratevars b where b.topic_id=$topic_id ) ORDER BY a.var_name";
     
    NightMare49, Feb 21, 2009 IP
  6. AmazingbLu

    AmazingbLu Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    use mysql5.x that should resolve your problem :p as mwasif said
     
    AmazingbLu, Feb 21, 2009 IP
  7. emitind

    emitind Peon

    Messages:
    567
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thank you for everyone's help. Much appriciated.
     
    emitind, Feb 22, 2009 IP