报告多个子查询的查询

时间:2013-01-29 22:18:05

标签: mysql join inner-join

从带有子查询连接的表中获取多条记录

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_namechat_box.message变量。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

目前还不清楚你要做什么,但似乎你可以使用这样的东西:

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