dpm91915 2013-08-12 16:56
浏览 22
已采纳

在sql查询中不起作用

i have two tables in my db .. one is Messages and the other is Contacts... both tables contain the mobile_number field.. in Contacts table there is no duplicate numbers ... but in Messages tables there are several duplicate numbers in mobileNo field...what i am doing is write now i am selecting the distinct mobile numbers from the Messages tables and then i am comparring the distinct numbers from the contacts table ... so if the messages_mobileNo is found on the contacts table then give me the contact name against the number otherwise messages_mobileNo ... so the problem is distinct not working .. i am not able to get the distinct numbers from the Messages table ... it is showing me the duplicate numbers

here is my query

 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
  • 写回答

3条回答 默认 最新

  • dongyi1939 2013-08-12 17:01
    关注

    So your trying to get the last 6 messages of a person if I'm right?

     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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,