Home > Article > Backend Development > How to use php case when query statement
php case when query statement is used to make a judgment similar to if else on the query results. Its usage syntax is such as "SELECT a.website_id...WHEN d.websitetype_id=1 THEN 400. ..END as money FROM …”.
The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.
How to use php case when query statement?
CASE WHEN usage in PHP Mysql
In the SELECT statement query, you can use CASE WHEN to make a judgment similar to if else on the query results.
Specific usage
1.
SELECT a.website_id, b.customer_name, a.website_enddate, c.member_name, d.websitetype_id, CASE WHEN d.websitetype_id=1 THEN 400 WHEN d.websitetype_id=2 THEN 400 WHEN d.websitetype_id=12 THEN 5800 WHEN d.websitetype_id=13 THEN 5800 WHEN d.websitetype_id=13 THEN 5800 END as money FROM ……
2.
SELECT a.website_id, b.customer_name, a.website_enddate, c.member_name, d.websitetype_id, CASE d.websitetype_id WHEN 1 THEN 400 WHEN 2 THEN 400 WHEN 12 THEN 5800 WHEN 13 THEN 5800 WHEN 13 THEN 5800 END as money FROM ……
3. A simple IF judgment
IF (judgment statement , the value returned by true, the value returned by false) The usage position is the same as the CASE WHEN position. The syntax is very simple, a bit like the ternary operation in PHP.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to use php case when query statement. For more information, please follow other related articles on the PHP Chinese website!