dongwen9051 2016-09-29 12:07
浏览 534
已采纳

如何在SQL上创建if else语句?

Hi Guys I'm having a Problem on how i could make a if else statement on SQL, It only displays the value of rent.rent_status = 1 on a Modal, but if rent.rent_status has no value the modal wont display, how can i make it display? Here is my code for your reference;

        $sql = "SELECT * FROM stall 
        LEFT JOIN tenant ON tenant.stall_id = stall.stall_id 
        LEFT JOIN rent ON rent.tenant_id = tenant.tenant_id
        WHERE rent.rent_status = 1 AND stall.stall_id = 1";
  • 写回答

3条回答 默认 最新

  • doumeng3345 2016-09-29 12:12
    关注

    You are using LEFT JOIN and then undoing the outer join in the WHERE clause. You need to move the conditions to the appropriate ON clauses:

    SELECT *
    FROM stall s LEFT JOIN
         tenant t 
         ON t.stall_id = s.stall_id LEFT JOIN
         rent r
         ON r.tenant_id = t.tenant_id AND r.rent_status = 1
    WHERE s.stall_id = 1;
    

    A LEFT JOIN keeps all rows in the first table along with matching rows in the second table. If there is no match, then the values are NULL for the columns in the second table. Your WHERE condition fails on NULL conditions.

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

报告相同问题?

悬赏问题

  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画