添加子查询

时间:2011-12-15 17:59:38

标签: mysql sql

我有以下查询:

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'

我正在尝试添加另一个子查询,但每次将其添加到子查询的末尾时,我在运行命令时都会收到错误。

这是我想要添加的内容。

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

运行时不断收到错误消息:

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'

1 个答案:

答案 0 :(得分:3)

您有两个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
/*---^^^^^^^^^^^^*/

第一个不应该在那里。

...
(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
...