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 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化