使用子查询联接从表中获取多条记录
SELECT
COUNT(*) AS total_count,
(SELECT
chat_box.user_id,
chat_box.message,
members.id,
members.display_name
FROM chat_box INNER JOIN members
ON chat_box.user_id = members.id
ORDER BY chat_id DESC LIMIT 1),
(SELECT COUNT(DISTINCT user_id) FROM chat_box) AS users_count
FROM chat_box
这就是我目前所拥有的,我想从内部连接获取
members.display_name
,其中chat_box.user_id = members.id
作为chat_box.message
旁边的输出,并将members.display_name
和chat_box.message
保存到一个变量。如有任何帮助,我们将不胜感激。 最佳答案
目前还不清楚你到底想做什么,但似乎你可以用这样的方法:
select u.user_id,
u.message,
u.id,
u.display_name,
cb1.total_count,
cb1.users_count
from
(
SELECT cb.user_id ,
cb.message,
m.id,
m.display_name
FROM chat_box cb
INNER JOIN members m
ON cb.user_id = m.id
) u
CROSS JOIN
(
select COUNT(*) AS total_count,
COUNT(DISTINCT user_id) AS users_count
FROM chat_box
) cb1
关于mysql - 报告查询多个子查询,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14593107/