Home >Backend Development >PHP Problem >How to use php case when query statement

How to use php case when query statement

藏色散人
藏色散人Original
2022-11-22 09:24:531136browse

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 …”.

How to use php case when query statement

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn