dongzi4030 2012-06-18 17:48
浏览 225
已采纳

获取查询结果行是否包含SELECT字段

The query below returns rows that have both loginid and ip2 in the bumps table.

All rows in the bumps table have ip2, but only some have loginid.

How can I get the query below to return ip2 for all rows and loginid for the rows that have loginid?

$sqlStrend = "SELECT e.loginid, e.time, l.username, e.ip2
      FROM bumps e
      JOIN login l ON e.loginid = l.loginid
     WHERE e.submissionid = '$submissionid' 
  ORDER BY e.time DESC
  LIMIT $offset, $rowsperpage";
  • 写回答

2条回答 默认 最新

  • dsqnonh2763 2012-06-18 17:51
    关注

    Rather than an INNER JOIN (implied by JOIN), you just need to change this to a LEFT JOIN, which returns all rows on the left-hand table and those related or NULL where rows in the related table do not exist.

    SELECT e.loginid, e.time, l.username, e.ip2
    FROM bumps e
    LEFT JOIN login l ON e.loginid = l.loginid
    WHERE e.submissionid = '$submissionid' 
    ORDER BY e.time DESC
    LIMIT $offset, $rowsperpage
    

    Review the different JOIN types in this excellent Wikipedia article or Jeff Atwood's Visual Explanation of Joins.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部