dpgbh20688 2015-09-05 14:51
浏览 78

左连接子查询返回Null

I am applying Left Join with Sub query & where clause

It seems fine no syntax error but the columns I am selecting from that sub query always returns me Null. I have executed the same part in my SQL section, it gives me record. Kindly have a look on the query and let me know if any thing is possible or if question is not clear.

SELECT alt.userId, u.name, t.name AS teamName, alt.startDateTime, v.name AS     villageName, c.name AS clusterName, startLat, startLong, latlng.lat, latlng.long 
FROM activity_log_tim AS alt 
JOIN user AS u ON u.userId = alt.userId
JOIN team_members AS tm ON tm.memberId = u.userId
JOIN team AS t ON t.teamId = tm.teamId
JOIN village AS v ON v.villageId = alt.villageId
JOIN cluster_villages AS cv ON cv.villageId = v.villageId
JOIN cluster AS c ON c.clusterId = cv.clusterId
LEFT JOIN (SELECT lat, long,dateTime, scheduleId FROM activity_log_gps LIMIT 1) AS latlng ON latlng.scheduleId = alt.scheduleId
WHERE DATE(alt.startDateTime) = '2015-09-05' AND DATE(alt.endDateTime) = '0000-00-00' GROUP BY alt.userId ORDER BY latlng.dateTime DESC 
  • 写回答

1条回答 默认 最新

  • duanbai1027 2015-09-05 14:58
    关注

    Well it's supposed to since you are performing a LEFT JOIN which produces NULL if no match occurs. So either you can perform a INNER JOIN or use a COALESCE function like below

       SELECT alt.userId, u.name, 
    t.name AS teamName, 
    alt.startDateTime, 
    v.name AS     
    villageName, 
    c.name AS clusterName, 
    startLat, 
    startLong, 
    COALESCE(latlng.lat, 23), //Notice the use of COALESCE. If null will return 23 as default 
    COALESCE(latlng.long, 32)
    
    评论

报告相同问题?

悬赏问题

  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失