1

使用子查询连接从表中获取多条记录

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

4

1 回答 1

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  
于 2013-01-29T22:27:54.427 回答