简体   繁体   English

MySQL不要两次选择相同的ID

[英]MySQL don't select same id twice

I'm working on a message system, and i can't figure this out! 我正在开发消息系统,但我无法弄清楚! The message system is built up something like this, 邮件系统是这样构建的,

ID
MESSAGE_ID
MESSAGE
FROM_USER
TO_USER

The message_id can be the same multiple times, thats the ID the from and to users are reading from while checking messages (so they can see all the messages that has been written). message_id可以多次相同,即在检查消息时from和to用户正在读取的ID(以便他们可以查看所有已写入的消息)。 But, i need to select all the messages, but i want to skip a row, if the ID has been select before. 但是,我需要选择所有消息,但是,如果之前已选择ID,我想跳过一行。 Is this possible in MySql? 这可能在MySql中吗? or do i need to run a array then remove all duplicated id's? 还是我需要运行一个数组然后删除所有重复的ID?

SELECT `ID`, `MESSAGE_ID`, `MESSAGE`, `FROM_USER`, `TO_USER`
FROM `YourDbTable`
GROUP BY `ID`, `MESSAGE_ID`

Group By may be what you are looking for... 分组依据可能是您正在寻找的...

您可以使用group by message_id

Maybe you can try something like this - 也许您可以尝试这样的事情-

SELECT    DISTINCT (message_id), message, id, 
          from_user, to_user
FROM      test
GROUP BY  message_id
ORDER BY  id; 

A live demo is given here . 这里提供了现场演示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM