这是我更大的主要查询的子查询:
SELECT DISTINCT p1.item_id
FROM pages_likes AS p1
JOIN pages_likes AS p2 ON p1.item_id = p2.item_id AND p1.status = p2.status
WHERE p1.uid = 855 AND p2.uid = 1091
GROUP BY p1.id
这给了我这个结果,例如:
item_id
32
38
73
所以我需要的预期计数值是3。
我需要知道是否可以在不使用子查询(不使用外部SUM()
)和的情况下获取找到的行而不发送额外的mysql查询 (不使用SELECT FOUND_ROWS()
)。
这是因为我不能将主列的值/列放在我的主查询中需要的子查询子查询中。那么,这个查询是否可以通过单个查询计算所有找到的行?
我的主要疑问是:
SELECT SQL_CALC_FOUND_ROWS DISTINCT l1.item_id, l1.uid, l2.id, l2.uid, u.prename, l1.item_id, l2.item_id,
(SELECT SUM(cnt) FROM
(
SELECT DISTINCT p1.item_id
FROM pages_likes AS p1
JOIN pages_likes AS p2 ON p1.item_id = p2.item_id AND p1.status = p2.status
WHERE p1.uid = 855 AND p2.uid = 1091
GROUP BY p1.id
) AS t) AS total
FROM pages_likes l1
JOIN users u on u.id = l1.uid
JOIN pages_likes l2 on l1.item_id = l2.item_id
JOIN users_likes ul on l1.uid = ul.uid
WHERE ul.date >= DATE_SUB(NOW(),INTERVAL 1 WEEK)
AND l1.uid != 1091 AND l2.uid = 1091
AND (l1.status = 1 AND l2.status = 1)
AND u.gender = 2
GROUP BY l1.uid
ORDER BY
total DESC,
l1.uid DESC,
l1.date DESC
我的目标是在主查询的子查询中使用l1.uid
而不是855
。
答案 0 :(得分:1)
如果我理解正确,只需使用此子查询:
(SELECT COUNT(DISTINCT p1.item_id)
FROM pages_likes p1 JOIN
pages_likes p2
ON p1.item_id = p2.item_id AND p1.status = p2.status
WHERE p1.uid = 855 AND p2.uid = 1091
)