0

我有以下联合查询,联合的第一部分返回 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'
4

1 回答 1

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;
于 2013-09-10T11:17:40.290 回答