Ronnel.D Ronnel.D - 1 year ago 64
PHP Question

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?
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";

Answer Source

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:

     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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download