我对以下查询有疑问。对于返回总结果总和的子查询,我收到错误Unknown column 'search.term' in 'where clause',我不确定如何正确执行此查询?任何帮助表示赞赏。
SELECT *,
(SELECT SUM(totalResults)
FROM (SELECT COUNT(*) as totalResults
FROM media
LEFT JOIN users ON users.id = media.userId
WHERE media.title LIKE CONCAT('%', search.term, '%')
OR media.description LIKE CONCAT('%', search.term, '%')
OR media.tags LIKE CONCAT('%', search.term, '%')
OR users.username LIKE search.term
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 CONCAT('%', search.term, '%')
OR media.description LIKE CONCAT('%', search.term, '%')
OR media.tags LIKE CONCAT('%', search.term, '%')
OR users.username LIKE search.term
)
t) AS totalResults,
MAX(search.timestamp) AS searchTimestamp
FROM search
WHERE userId = '1'
GROUP BY search.term
ORDER BY searchTimestamp DESC