我有以下查询,我在网上找到了它的修改版本。我有另一个名为contacts的表,我正在尝试使用c.email内部加入l.id。我通常不使用子查询,所以我在网上找到的东西对我来说似乎很陌生。
在任何情况下,如何在l.id = c.lead_id中添加内连接,因为我需要选择c.email。
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
FROM leads AS l
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'
我知道,问题不明确,但任何人都可以提供帮助。
答案 0 :(得分:1)
你真的很亲密:
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,
c.email
FROM leads AS l
INNER JOIN contacts AS c ON l.id = c.lead_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'
INNER JOIN就像添加另一个表一样,首先你有原始的“FROM”表,然后是INNER JOIN(或其他变种)。