我的数据库中有两个表..一个是消息,另一个是联系人...两个表都包含mobile_number字段..在联系人表中有没有重复的号码...但是在“消息”表中,“mobileNo”字段中有几个重复的号码...我正在做的是现在写,我从消息表中选择不同的手机号码,然后我比较联系人表中的不同号码...因此,如果在联系人表中找到 messages_mobileNo,则将联系人姓名与号码相对应,否则为 messages_mobileNo ...所以问题明显无法正常工作..我无法得到消息表中的不同数字...它向我显示了重复的数字
这是我的查询
SELECT DISTINCT Message.mobileNo,
Contact.mobileNo,
Contact.workNo,
Contact.homeNo,
Contact.other,
Contact.name,
Message.body,
Message.idTextMessage
FROM cakephp_db.textmessage AS Message
LEFT JOIN cakephp_db.contacts AS Contact ON (Message.user_id = Contact.user_id
AND ((Message.mobileNo = Contact.mobileNo)
OR (Message.mobileNo = Contact.workNo)
OR (Message.mobileNo = Contact.homeNo)
OR (Message.mobileNo = Contact.other)))
WHERE Message.User_id = 23
ORDER BY Message.idTextMessage DESC LIMIT 6
最佳答案
如果我是对的,你想获取某人的最后 6 条消息吗?
SELECT Message.mobileNo,
Contact.mobileNo,
Contact.workNo,
Contact.homeNo,
Contact.other,
Contact.name,
Message.body,
Message.idTextMessage
FROM cakephp_db.textmessage AS Message
LEFT JOIN cakephp_db.contacts AS Contact ON Message.user_id = Contact.user_id
AND Message.mobileNo IN (Contact.mobileNo, Contact.workNo, Contact.homeNo, Contact.other)
WHERE Message.User_id = 23
GROUP BY Message.mobileNo
ORDER BY Message.idTextMessage DESC LIMIT 6
关于php - 不同的在sql查询中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18192554/