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 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)