我正在编写一个MySQL查询,该查询获取潜在客户列表(提交表单中的潜在客户)和该订单的乘客数量,其中潜在客户详细信息缺失或为空或= default
。所以我加入了lead_detail表,但只知道如何检查它是否存在,而不是它是否存在。这是我到目前为止 -
SELECT l1.id, (
/* sub query to get the passenger count from field 22 */
SELECT lm1.`value` FROM lead_detail lm1
WHERE lm1.form_id = l1.form_id AND lm1.lead_id = l1.id
AND lm1.field_number = 22
) passengers FROM lead l1
/* join our detail table */
LEFT JOIN lead_detail lm2 ON (l1.id = lm2.lead_id)
WHERE lm2.field_number = 29 AND lm2.form_id = 1
AND lm2.lead_id = l1.id AND (lm2.`value` = 'default' OR lm2.`value` = '')
/* make sure we are on lead form 1 and they have paid */
AND l1.form_id = 1 AND l1.payment_status = 'Paid'
仅作为参考,字段编号 22 =乘客数量
字段编号 29 =表单访问密钥(空,default
或不存在)
值得注意的是,这是Gravity Forms WordPress插件,但这绝对是我想要实现的自定义操作。下面的lm2表结构 -
(int) id
(int) lead_id
(int) form_id
(int) field_number
(string) value
希望很清楚我想要做什么。谢谢!
答案 0 :(得分:1)
您应该更改lead_detail
子查询的别名,让我们说lm2
,或者更改主查询中同一个表的别名。例如:
/* sub query to get the passenger count from field 22 */
SELECT lm2.`value` FROM lead_detail lm2
WHERE lm2.form_id = l.form_id AND lm2.lead_id = l.id
AND lm2.field_number = 22