我有以下联合查询,它为联合的第一部分返回8,为第二部分返回1。我想知道如何合并总数,结果是9。
这是查询
SELECT COUNT(*)
FROM media
LEFT JOIN users ON users.id = media.userId
WHERE media.title LIKE '%braydo25%' OR media.description LIKE '%braydo25%' OR media.tags LIKE '%braydo25%' OR users.username LIKE 'braydo25'
UNION ALL
SELECT COUNT(*)
FROM reposts
LEFT JOIN media ON media.id = reposts.mediaId
LEFT JOIN users ON users.id = reposts.userId
WHERE media.title LIKE '%braydo25%' OR media.description LIKE '%braydo25%' OR media.tags LIKE '%braydo25%' OR users.username LIKE 'braydo25'
答案 0 :(得分:2)
结合后的计数总结:
select sum(cnt)
from (SELECT COUNT(*) as cnt
FROM media
LEFT JOIN users ON users.id = media.userId
WHERE media.title LIKE '%braydo25%' OR media.description LIKE '%braydo25%' OR media.tags LIKE '%braydo25%' OR users.username LIKE 'braydo25'
UNION ALL
SELECT COUNT(*)
FROM reposts
LEFT JOIN media ON media.id = reposts.mediaId
LEFT JOIN users ON users.id = reposts.userId
WHERE media.title LIKE '%braydo25%' OR media.description LIKE '%braydo25%' OR media.tags LIKE '%braydo25%' OR users.username LIKE 'braydo25'
) t;