Jump to content

LEFT JOIN and alias syntax error


whitedragon101

Recommended Posts

MYSQL Version : 5.0.91

 

I have two queries which run fine however when I alias them and left join them I get a syntax error :

 

QUERY_1

SELECT * FROM(

(SELECT lga_id , COUNT(school_id_p) as num_of_schools_unapproved FROM schools WHERE EXISTS ( SELECT * FROM orders WHERE approved = 'no' AND schools.school_id_p = orders.school_id) GROUP BY lga_id) AS table1

LEFT JOIN

(SELECT lga_id_p, name FROM lga) AS table2


ON table1.lga_id = table2.lga_id_p

)

 

produces:

 

lga_id num_of_schools_unapproved lga_id_p name

2 2 2 lga_name2

4 1 4 lga_name4

5 1 5 lga_name

 

which is correct

 

QUERY_2

SELECT lga_id , COUNT(schools.school_id_p) as num_of_schools FROM schools GROUP BY lga_id

 

produces :

 

lga_id num_of_schools

1 5

2 5

3 5

4 5

5 5

6 3

7 2

 

which is correct.

 

 

However when I try to call them table3 and table 4 and do a LEFT JOIN on the lga_id I get a syntax error.  Any help much appreciated :

 

SELECT * FROM(


SELECT * FROM(

(SELECT lga_id , COUNT(school_id_p) as num_of_schools_unapproved FROM schools WHERE EXISTS ( SELECT * FROM orders WHERE approved = 'no' AND schools.school_id_p = orders.school_id) GROUP BY lga_id) AS table1

LEFT JOIN

(SELECT lga_id_p, name FROM lga) AS table2


ON table1.lga_id = table2.lga_id_p

) AS table3 LEFT JOIN


(SELECT lga_id , COUNT(schools.school_id_p) as num_of_schools FROM schools GROUP BY lga_id) AS table4

ON table3.lga_id = table4.lga_id

)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.