简体   繁体   English

添加子查询

[英]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 您有两个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
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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