开发者

Creating a new parameter based on a logic in MySQL

开发者 https://www.devze.com 2023-03-20 00:17 出处:网络
My MySQL table contains 2 fields and I want to create a \"new parameter\" based on a logic to apply ORDER BY \'new parameter\' function in MySQL.

My MySQL table contains 2 fields and I want to create a "new parameter" based on a logic to apply ORDER BY 'new parameter' function in MySQL.

My logic to create the "new parameter":

+------------+------------+
| field1     |  field2    |
+------------+------------+
| 100        |            |
| 101        |            |
| 103        | 101        |
| 105        |            |
| 110        | 100        |
| 115        | 110        |
| 116        | 100        |
+------------+------------+

Logic:

If 'field2' is NULL then take 'field1' value
Else take 'field2' value 

after applyi开发者_如何学Pythonng this logic "new parameter" will contain these values for each row in the table:

100
101
101
105
100
110
100

How can I do this in MySQL/PHP. Thanks.


SELECT * FROM TABLE WHERE COALESCE(Field2, Field1) = 'Blah Blah'

The COALESCE Function will move through its list of fields/values until it finds a non DBNULL value. COALESCE can also be used in the Return Section of a Statement.

SELECT COALESCE(Field2, Field1) as NewParam FROM TABLE 
0

精彩评论

暂无评论...
验证码 换一张
取 消