Connecting Tech Pros Worldwide Help | Site Map

MySQL Subselect

Newbie
 
Join Date: Apr 2009
Posts: 1
#1: Apr 27 '09
This may seem simple to most, but for some reason i cannot get this to work!!

I have 2 tables, table_a & table_b

Both tables have a string reference number used to join. table_a will only ever have one occurance of the reference number, where as table_b has multiple records with the same reference reference number, ordered by a column called 'order'.

I am trying to pull out all records in table_a and the corresponding record in table_b with an order of '1'. There will always only be one record with order of '1' in table_b for each record in table_a.

In MSSQL I would do something like:

select a.reference_no, b.order, b.date
from table_a a
left join (select reference_no,order,date from table_b) b on a.reference_no=b.reference_no
where b.order=1

In MySQL it doesn't work! I am running version 5.0 MySQL server which is supposed to support subqueries, but I keep getting a syntax error when I try to execute it. The syntax error always refers to the subselect query.

Any ideas?

Many thanks!
blyxx86's Avatar
Familiar Sight
 
Join Date: Nov 2006
Posts: 255
#2: May 7 '09

re: MySQL Subselect


Expand|Select|Wrap|Line Numbers
  1. SELECT a.reference_no, b.order, b.date
  2. FROM table_a AS a
  3. LEFT JOIN table_b AS b ON a.reference_no=b.reference_no
  4. WHERE b.order=1
  5.  
You don't actually need to define the SELECT statement within the join. Unless I am missing what you are trying to do.

:)
Reply

Tags
mysql join subselect, mysql nested select, mysql subselect