English 中文(简体)
由MySQL Prob编辑的小组
原标题:Group By/Threaded Messaging MySQL Prob

I m 建造一个私人电传系统,用于一个ating点。 下表的结构:

`id` bigint (20) NOT NULL AUTO_INCREMENT , 
`fromme` integer (11) NOT NULL, 
`tome` integer (11) NOT NULL, 
`subject` varchar (255) NOT NULL, 
`message` longtext NOT NULL, 
`mydate` datetime NOT NULL, 
`thread` varchar (255) NOT NULL, 
`receipt` varchar (50) NOT NULL, 
`INDELETE` varchar (5), 
`SENTDELETE` varchar (5),
PRIMARY KEY (`id`)

当用户向另一个用户发出新信息时,它会产生一种任意的插图,以跟踪透镜。 在他们答复时,它穿透镜头(与脸书相仿)。 当用户登录时,他们可以在其箱子里看到所有信息,并且根据它是否是一种新信息,它改变了浏览的颜色。 所有这一切都属于工作罚款,但收到的信息已复发。 问题是:

select messages.id, messages.fromme, messages.subject, messages.message, messages.receipt, messages.mydate, messages.thread, users.firstname, users.lastname, users.image1
from messages, users
where messages.tome =  40  and messages.INDELETE != y  and messages.fromme = users.id
GROUP BY messages.thread
ORDER BY messages.mydate desc

它适当地返回,但专家组正在按职能归还宇宙航行联合会的线索。 我需要LATEST,以便它能够适当开展工作。 任何人都知道如何做到这一点?

最佳回答

为了按条款获得一个小组的惠益,你需要一个整体功能(国歌、国歌、国联等)。 也许,你希望利用这一次顺序,在每一读物中提供最新的信息(最高度的ID):

select MAX(messages.id) id, messages.thread 
FROM messages
GROUP BY messages.thread

将所有可能使用的武器放在一起

SELECT messages.id, 
       messages.fromme, 
       messages.subject, 
       messages.message, 
       messages.receipt, 
       messages.mydate, 
       messages.thread, 
       users.firstname, users.lastname, users.image1
  FROM messages, 
       users,
       (SELECT MAX(messages.id) id, messages.thread thread 
          FROM messages
        GROUP BY messages.thread) latest,
 WHERE messages.tome =  40  
   AND messages.INDELETE != y  
   AND messages.fromme = users.id
   AND messages.id=latest.id
ORDER BY messages.mydate desc
问题回答

回答“Ollie Jones”是罚款的(但“Kingk”式的旧式“ com”式加入“yn”,但对于它在此的价值来说是一种替代:

SELECT m1.*, u.*
FROM messages m1
LEFT OUTER JOIN messages m2
  ON (m1.thread=m2.thread AND m1.mydate < m2.mydate)
JOIN users u ON (m1.fromme = u.id)
WHERE m2.thread IS NULL;

无需<条码>。 这与每一行<代码>m1与一个有相同读写和较晚日期的假设浏览代码/code>相符。 如果没有浏览m2,则m1 必须成为最新的信息。

这种办法对MySQL是好的,因为MySQL通常会设立一个临时表格,因为它在<代码>上分类。 由查询组。 加入的解决办法往往更快。 • 确保你有一个指数(阅读、阅读)。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签