Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL:将发生的行数与结果合并_Mysql_Inner Join_Mysql Num Rows - Fatal编程技术网

MySQL:将发生的行数与结果合并

MySQL:将发生的行数与结果合并,mysql,inner-join,mysql-num-rows,Mysql,Inner Join,Mysql Num Rows,我正在尝试为我的应用程序构建一个简单的私人消息系统。我设置消息的方式是,每条消息都有一个与之关联的线程\u散列。相互关联的消息具有相同的线程散列(回复其他消息的消息都具有相同的线程散列) 我能够运行一个查询,从每个组中选择最后一个输入的行(属于线程\u hash)我想要完成的是返回另一列,其中包含具有特定线程\u散列的行数,而不进行单独查询 我创建了一个SQL FIDLE,其中包含用于生成行的查询: 我仅有的信息是用户的ID。我没有线程散列,因此这必须从ID生成。我尝试过使用一些查询来生成行数,

我正在尝试为我的应用程序构建一个简单的私人消息系统。我设置消息的方式是,每条消息都有一个与之关联的
线程\u散列。相互关联的消息具有相同的
线程散列
(回复其他消息的消息都具有相同的
线程散列

我能够运行一个查询,从每个组中选择最后一个输入的行(属于
线程\u hash
我想要完成的是返回另一列,其中包含具有特定
线程\u散列的行数
,而不进行单独查询

我创建了一个SQL FIDLE,其中包含用于生成行的查询:


我仅有的信息是用户的ID。我没有
线程散列
,因此这必须从ID生成。我尝试过使用一些查询来生成行数,但我的大脑在这一小时无法正常工作。

我相信你在寻找这样的查询

SELECT COUNT(*) AS number_msg, thread_hash, from_user_id, mark_read, subject, SUBSTRING(message, 1, 65) as message, messages.time_stamp
FROM `messages`
WHERE `to_user_id` =  '28'
GROUP BY thread_hash
ORDER BY `messages`.`time_stamp` ASC
LIMIT 20 

您可以这样编写查询:

SELECT
  thread_hash,
  from_user_id,
  mark_read,
  subject,
  SUBSTRING(message, 1, 65) as message,
  messages.time_stamp,
  cnt
FROM
  `messages`
  JOIN (SELECT MAX(messages.id) thead_id, COUNT(*) cnt
        FROM messages
        WHERE messages.to_user_id = 28
        GROUP BY thread_hash) thread_head
  ON `messages`.`id` = `thread_head`.`thead_id`
WHERE `to_user_id` =  '28'
ORDER BY `messages`.`time_stamp` ASC
LIMIT 20
(SELECT MAX(CASE WHEN messages.to_user_id = 28 THEN messages.id END) thead_id,
        COUNT(*) cnt
 FROM messages
 GROUP BY thread_hash) thread_head
小提琴是一种乐器

但我不确定您是否需要只计算用户28的消息,还是所有消息。如果需要统计所有消息,可以通过以下方式重写子查询:

SELECT
  thread_hash,
  from_user_id,
  mark_read,
  subject,
  SUBSTRING(message, 1, 65) as message,
  messages.time_stamp,
  cnt
FROM
  `messages`
  JOIN (SELECT MAX(messages.id) thead_id, COUNT(*) cnt
        FROM messages
        WHERE messages.to_user_id = 28
        GROUP BY thread_hash) thread_head
  ON `messages`.`id` = `thread_head`.`thead_id`
WHERE `to_user_id` =  '28'
ORDER BY `messages`.`time_stamp` ASC
LIMIT 20
(SELECT MAX(CASE WHEN messages.to_user_id = 28 THEN messages.id END) thead_id,
        COUNT(*) cnt
 FROM messages
 GROUP BY thread_hash) thread_head
请看小提琴