SQL:在where子句中不同并获取结果

时间:2017-05-23 08:11:48

标签: mysql

我在过去2小时内尝试了这个并使用了许多方法,如MIN(),DISTINCT和CONCAT(),但无法获得我想要的结果。需要专家来看看我的代码并告诉我我在哪里做错了。

我的数据库结构


ID  | message_security_code | employer_ID | seeker_ID | chat_message  | sent_by  | dated
---------------------------------------------------------------------------------------------
1   | 66666666              |  45         |  78       |  hello        | employer | 2017-05-23
2   | 44444444              |  45         |  78       |  hello        | seeker   | 2017-05-23
3   | 55555555              |  45         |  78       |  hello        | employer | 2017-05-23
4   | 66666666              |  45         |  78       |  hello        | employer | 2017-05-23

我想获取所有结果,但只有一条记录会显示message_security_code最新的一个订单ID是否也是employee_ID =我的给定ID

这是我几个小时的尝试

  

尝试1

"SELECT DISTINCT message_security_code FROM pp_chat_messages WHERE employer_ID = '$id'"

  

尝试2

"SELECT MIN(ID) AS ID, employer_ID, seeker_ID, chat_message, sent_by, dated FROM pp_chat_messages WHERE employer_ID = '$id' GROUP BY message_security_code"

预期结果:如果employer_ID为45

ID  | message_security_code | employer_ID | seeker_ID | chat_message
----------------------------------------------------------------------
1   | 66666666              |  45         |  78       |  hello
2   | 44444444              |  45         |  78       |  hello
3   | 55555555              |  45         |  78       |  hello

0 个答案:

没有答案