如果我从下面的语句中删除ORDER BY c.campaign_id ASC
它可以正常工作,但现在它会抛出Sub query return more than 1 row
。
SELECT SQL_CALC_FOUND_ROWS c.campaign_id,
c.title as campaign_title,
IFNULL((SELECT meta_value
FROM campaign_meta as cm
WHERE cm.campaign_id = c.campaign_id
AND cm.meta_key = 'total_viewed'), 0) AS total_viewed,
c.campaign_identifier,
c.added_datetime,
l.label_id,
l.title AS label_title,
l.color,
u.username
FROM campaigns AS c
LEFT JOIN users AS u ON u.user_id = c.author
LEFT JOIN campaign_relation AS cr ON c.campaign_id = cr.campaign_id
LEFT JOIN labels AS l ON l.label_id = cr.label_id
ORDER BY c.campaign_id ASC
LIMIT 0, 10
答案 0 :(得分:2)
您的子查询返回的结果多了一行
(SELECT meta_value FROM campaign_meta as cm WHERE cm.campaign_id = c.campaign_id AND
cm.meta_key = 'total_viewed')
我们需要更改您的查询以使用sytax:
SELECT ... LIMIT 1
为MySQL更新它
(SELECT meta_value FROM campaign_meta as cm WHERE cm.campaign_id = c.campaign_id AND
cm.meta_key = 'total_viewed' LIMIT 1)
或重写查询
对MS SQL语法感兴趣
(SELECT TOP 1 meta_value FROM campaign_meta as cm WHERE cm.campaign_id = c.campaign_id AND
cm.meta_key = 'total_viewed')