简体   繁体   中英

Adding a Sub-Query

I have the following query:

SELECT l.id, 
(SELECT amount FROM lead_status WHERE lead_id = l.id AND buyer = 'BUYER_PROXY') AS our_bid,
(SELECT amount FROM lead_status WHERE lead_id = l.id AND buyer != 'BUYER_PROXY' AND discriminator='AUTO_PING' ORDER BY amount DESC LIMIT 1) AS best_bid,
(SELECT amount FROM lead_status WHERE lead_id = l.id AND discriminator = 'AUTO_POST' AND winner = 1 ORDER BY amount DESC LIMIT 1) AS final_sold_amount,
(SELECT buyer FROM lead_status WHERE lead_id = l.id AND discriminator = 'AUTO_POST' AND winner = 1 ORDER BY amount DESC LIMIT 1) AS buyer,
(SELECT COUNT(*) FROM vehicles WHERE lead_id = l.id) AS number_of_vehicles FROM leads AS l,
co.first_name, co.last_name, co.email, co.state, co.credit_type, ca.make, ca.model, co.owns_home, 
co.months_in_residence, a.injury, a.at_fault, d.gender_type, d.license_valid, d.license_obtained_age,
d.education_level, d.license_revoked_in_last_five_years, TIME(l.create_date), rc.coverage_type
FROM leads AS l
LEFT JOIN contacts AS co ON l.id = co.lead_id
LEFT JOIN vehicles AS v ON v.lead_id = l.id
LEFT JOIN cars AS ca ON ca.id = v.car_id
LEFT JOIN drivers AS d ON d.lead_id = l.id
LEFT JOIN accidents AS a ON d.id = a.driver_id
LEFT JOIN requested_coverage AS rc ON rc.lead_id = l.id
WHERE l.state = 'ACCEPTED'
AND (SELECT buyer FROM lead_status WHERE lead_id = l.id AND discriminator = 'AUTO_POST' AND winner = 1 ORDER BY amount DESC LIMIT 1) != 'TEST'
AND DATE(l.create_date) > '2011-12-01'

I am trying to add another subquery, but each time I add it to the end of the sub queries I get an error when I run the command.

Here's what I'm trying to add.

(SELECT COUNT(*) FROM vehicles WHERE lead_id = l.id) AS number_of_vehicles FROM leads AS l 

Keep getting an error message when I run:

SELECT l.id, 
(SELECT amount FROM lead_status WHERE lead_id = l.id AND buyer = 'BUYER_PROXY') AS our_bid,
(SELECT amount FROM lead_status WHERE lead_id = l.id AND buyer != 'BUYER_PROXY' AND discriminator='AUTO_PING' ORDER BY amount DESC LIMIT 1) AS best_bid,
(SELECT amount FROM lead_status WHERE lead_id = l.id AND discriminator = 'AUTO_POST' AND winner = 1 ORDER BY amount DESC LIMIT 1) AS final_sold_amount,
(SELECT buyer FROM lead_status WHERE lead_id = l.id AND discriminator = 'AUTO_POST' AND winner = 1 ORDER BY amount DESC LIMIT 1) AS buyer,
(SELECT COUNT(*) FROM vehicles WHERE lead_id = l.id) AS number_of_vehicles FROM leads AS l,
co.first_name, co.last_name, co.email, co.state, co.credit_type, ca.make, ca.model, co.owns_home, 
co.months_in_residence, a.injury, a.at_fault, d.gender_type, d.license_valid, d.license_obtained_age,
d.education_level, d.license_revoked_in_last_five_years, TIME(l.create_date), rc.coverage_type
FROM leads AS l
LEFT JOIN contacts AS co ON l.id = co.lead_id
LEFT JOIN vehicles AS v ON v.lead_id = l.id
LEFT JOIN cars AS ca ON ca.id = v.car_id
LEFT JOIN drivers AS d ON d.lead_id = l.id
LEFT JOIN accidents AS a ON d.id = a.driver_id
LEFT JOIN requested_coverage AS rc ON rc.lead_id = l.id
WHERE l.state = 'ACCEPTED'
AND (SELECT buyer FROM lead_status WHERE lead_id = l.id AND discriminator = 'AUTO_POST' AND winner = 1 ORDER BY amount DESC LIMIT 1) != 'TEST'
AND DATE(l.create_date) > '2011-12-01'

You have two FROM leads AS l

(SELECT COUNT(*) FROM vehicles WHERE lead_id = l.id) AS number_of_vehicles FROM leads AS l,
/* ------------------------------------------------------------------------------^^^^^^^^*/
co.first_name, co.last_name, co.email, co.state, co.credit_type, ca.make, ca.model, co.owns_home, 
co.months_in_residence, a.injury, a.at_fault, d.gender_type, d.license_valid, d.license_obtained_age,
d.education_level, d.license_revoked_in_last_five_years, TIME(l.create_date), rc.coverage_type
FROM leads AS l
/*---^^^^^^^^^^^^*/

The first one should not be there.

...
(SELECT COUNT(*) FROM vehicles WHERE lead_id = l.id) AS number_of_vehicles,
co.first_name, co.last_name, co.email, co.state, co.credit_type, ca.make, ca.model, co.owns_home, 
co.months_in_residence, a.injury, a.at_fault, d.gender_type, d.license_valid, d.license_obtained_age,
d.education_level, d.license_revoked_in_last_five_years, TIME(l.create_date), rc.coverage_type
FROM leads AS l
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM