我有这个问题,它不会给我带来任何错误。每个子查询在单独运行时按预期工作。当我运行整个事情时,如下所示,我只得到列:post_title
& views
SELECT post_title,
views,
coupons_today,
coupons_lifetime,
coupons_remaining
FROM wp_posts p
LEFT JOIN wp_act ct --has views column
ON ct . campaign_id = p . id
LEFT JOIN(SELECT Count(*) AS coupons_today,
campaign_id cid
FROM wp_act
WHERE date = CURRENT_DATE) AS act1
ON act1 . cid = p . id
LEFT JOIN(SELECT Count(*) AS coupons_lifetime,
campaign_id cid2
FROM wp_act) AS act2
ON act2 . cid2 = p . id
LEFT JOIN(SELECT Count(*) AS coupons_remaining,
campaign_id cid3
FROM wp_ac
WHERE status IS NULL) AS ac
ON p . id = ac . cid3
WHERE p . post_status = 'publish'
AND p . post_type = 'page'
AND p . ping_status = 'open';
答案 0 :(得分:0)
以这种方式尝试:
SELECT p.post_title,
ct.views,
( SELECT Count(*)
FROM wp_act
WHERE date = CURRENT_DATE
AND campaign_id = p.id
) AS coupons_today,
( SELECT Count(*)
FROM wp_act
WHERE campaign_id = p.id
) AS coupons_lifetime,
( SELECT Count(*)
FROM wp_ac
WHERE status IS NULL
AND campaign_id = p.id
) AS coupons_remaining
FROM wp_posts p
LEFT JOIN wp_act ct ON ct . campaign_id = p . id
WHERE p . post_status = 'publish'
AND p . post_type = 'page'
AND p . ping_status = 'open';
答案 1 :(得分:0)
在朋友的帮助下(感谢Swaroop)我最终得到了这个:
SELECT p.post_title,
ct.views AS views,
ct.coupons AS coupons_today,
Sum(ctl.coupons) AS coupons_lifetime,
(SELECT Count(*) FROM wp_ac WHERE c.campaign_id = p.id AND c.status IS NULL)
AS coupons_remaining
FROM wp_posts p
LEFT JOIN wp_act ct ON ct.campaign_id = p.id AND ct.date = CURRENT_DATE
LEFT JOIN wp_act ctl ON ctl.campaign_id = p.id
WHERE p.post_status = 'publish'
AND p.post_type = 'page'
AND p.ping_status = 'open'
GROUP BY p.id