doudong3570 2014-05-10 07:55
浏览 69
已采纳

带有左连接的sql查询并阻止空结果(数据表)

I am using datatables with a modified ssp.class.php to allow for joins and other custom features. In the example below I only want to return results from computers for the first x rows sorted by their id. Because of this, I list computers with my conditions first then LEFT JOIN users followed by logs (the information I am after).

It works great, BUT because of the left joins I have empty results. For instance, while my result set only contains logs from the correct computers... if logs has no rows for a particular user/computer combination I have a row with empty log data, but with user/computer data which serves me no purpose.

Is my only option to include a WHERE condition to prevent null values in the logs join... WHERE logs.user_id != '' or is there some other logic I can do in the select that I am missing?

SELECT (*see note)
FROM
    ( SELECT account_id, computer_id, computer_name
        FROM computers
        ORDER BY computer_id ASC LIMIT 0, ".$_SESSION['user']['licenses']."
    ) as c
    LEFT JOIN users
        on users.computer_id = c.computer_id
    LEFT JOIN logs
        on logs.user_id = users.user_id
  • 写回答

2条回答 默认 最新

  • dongrou5254 2014-05-10 08:07
    关注

    You can use just JOIN for the table logs.

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

报告相同问题?

悬赏问题

  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题