我正在尝试运行一个连接3个表的查询,我想将第一个连接限制为仅5行。最终结果可以返回任意数量的行,因此我不想将LIMIT添加到查询的末尾。
这是我的查询,它有效,但显然不会将第一次连接限制为5行。我尝试过一个子查询,我相信这是完成此任务的唯一方法,我尝试的一切都会出错。我似乎无法应用我见过的例子,我的情况。
SELECT mw_customer.customer_id, mw_customer.customer_uid, mw_campaign.customer_id, mw_campaign.campaign_id, mw_campaign.type, mw_campaign.status, mw_campaign_delivery_log.campaign_id, mw_campaign_delivery_log.subscriber_id
FROM mw_customer
JOIN mw_campaign
ON mw_customer.customer_id = mw_campaign.customer_id
AND mw_customer.customer_uid = 'XYZ'
AND mw_campaign.type = 'regular'
AND mw_campaign.status = 'sent'
JOIN mw_campaign_delivery_log
ON mw_campaign.campaign_id = mw_campaign_delivery_log.campaign_id
所以我想要做的是将“JOIN mw_customer”限制为最多5行,然后在JOIN mw_campaign_delivery_log之后,可以有任意数量的行。
由于
答案 0 :(得分:1)
使用LIMIT 5
在子查询中包装第一个连接。
SELECT t.customer_id, t.customer_uid, t.campaign_id, t.type, t.status, l.subscriber_id
FROM (SELECT cus.customer_id, cus.customer_uid, cam.campaign_id, cam.type, cam.status
FROM mw_customer AS cus
JOIN mw_campaign AS cam
ON cus.customer_id = cam.customer_id
WHERE cus.customer_uid = 'XYZ'
AND cam.type = 'regular'
AND cam.status = 'sent'
LIMIT 5) AS t
JOIN mw_campaign_delivery_log AS l
ON t.campaign_id = l.campaign_id
请注意,LIMIT
没有ORDER BY
意味着所选的5行将无法预测。