简体   繁体   English

如何在SQL上进行if else语句?

[英]How can I make a if else statement on SQL?

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? 嗨,大家好,我在SQL上如何执行if else语句时遇到问题,它仅在Modal上显示rent.rent_status = 1的值,但是如果rent.rent_status没有任何值,则不会显示模式,如何我让它显示吗? 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";

You are using LEFT JOIN and then undoing the outer join in the WHERE clause. 您正在使用LEFT JOIN ,然后在WHERE子句中撤消外部LEFT JOIN You need to move the conditions to the appropriate ON clauses: 您需要将条件移至适当的ON子句:

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. LEFT JOIN将所有行保留在第一个表中,并将匹配的行保留在第二个表中。 If there is no match, then the values are NULL for the columns in the second table. 如果不匹配,则第二个表中的列的值为NULL Your WHERE condition fails on NULL conditions. 您的WHERE条件在NULL条件下失败。

Maybe try Wrapping your output with code similar to this 也许尝试用与此类似的代码包装输出

while (mysqli_stmt_fetch($stmt)) {
    if(mysqli_stmt_num_rows($stmt) > 0){'output goes here'}
}

I'm not sure how you're using your php , this is just to give you an idea to grow on. 我不确定您如何使用php,这只是为了给您一个发展的想法。

    You don't need to use if..else just use or operator....   


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) OR (rent.rent_status IS NULL)) AND stall.stall_id = 1"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM