Jump to content


Photo

mysql sub-query question


  • Please log in to reply
3 replies to this topic

#1 gudmunson

gudmunson
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 07 June 2006 - 09:36 PM

Hey guys,

I am trying to write a sub-query to select a subset of data from my database but it is not working. Can somebody take a quick look to see if there is something simple I'm missing? I have tried the subquery by itself and it works, as does the outside query with one value: IN(2384)...so something is wrong. Is this not supported by mysql?

SELECT *
FROM Schools_equiv_request
WHERE reg_id
IN (
  SELECT sr.id
  FROM Students_registration sr
  LEFT JOIN Programs_terms pt ON pt.id = sr.term_id
  LEFT JOIN Programs p ON p.id = pt.program_id
  WHERE p.school_id =88
)

Here's what phpmyadmin tells me:

MySQL said: Documentation
#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 sr . id from Students_registration sr LEFT JOIN Progra

Any help would be appreciated, I've been working on this for longer than I'd like to admit [img src=\"style_emoticons/[#EMO_DIR#]/wink.gif\" style=\"vertical-align:middle\" emoid=\":wink:\" border=\"0\" alt=\"wink.gif\" /]

#2 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 09 June 2006 - 05:59 AM

Well, you already eliminated the obvious ones (no subquery support, error in the subquery itself), so I'm not really sure. It looks correct syntactically, but obviously, MySQL doesn't agree. What version are you running? And could you post the CREATE TABLE output for all tables involved? I'll take a closer look.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

#3 gudmunson

gudmunson
  • Members
  • PipPip
  • Member
  • 17 posts

Posted 09 June 2006 - 03:02 PM

[!--quoteo(post=381755:date=Jun 8 2006, 10:59 PM:name=fenway)--][div class=\'quotetop\']QUOTE(fenway @ Jun 8 2006, 10:59 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Well, you already eliminated the obvious ones (no subquery support, error in the subquery itself), so I'm not really sure. It looks correct syntactically, but obviously, MySQL doesn't agree. What version are you running? And could you post the CREATE TABLE output for all tables involved? I'll take a closer look.
[/quote]


I did a bit more research and I'm running mysql 4.0.1, and subqueries weren't supported until version 4.1, so that is my answer! I ended up making a join statement that did away with the subquery altogether. Thanks for the comment though.

#4 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 09 June 2006 - 05:01 PM

[!--quoteo(post=381868:date=Jun 9 2006, 11:02 AM:name=gudmunson)--][div class=\'quotetop\']QUOTE(gudmunson @ Jun 9 2006, 11:02 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
I did a bit more research and I'm running mysql 4.0.1, and subqueries weren't supported until version 4.1, so that is my answer! I ended up making a join statement that did away with the subquery altogether. Thanks for the comment though.
[/quote]
Glad you got it working -- when I first read your post, I got the impression that you _had_ testing subqueries. Whatever -- JOINs are better anyway.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users