SQL查找哪个OR匹配

时间:2016-02-06 15:54:16

标签: php mysql sql

在如 SELECT pluviometers.*, lastDate, lastValue, firstDate, firstValue, rain = CASE firstDate WHEN NULL THEN 0 ELSE (lastValue - firstValue) / (lastDate - firstDate) END FROM pluviometers LEFT JOIN ( SELECT h.pluviometer_id AS pid, MAX(h.date) AS lastDate, h.value AS lastValue FROM rains h LEFT JOIN ( SELECT h2.pluviometer_id AS pid2, MIN(h2.date) AS firstDate, h2.value AS firstValue FROM rains h2 WHERE h2.date > ###### GIVEN DATE ###### GROUP BY pid2 ORDER BY pid2 ASC ) AS p2 ON pid2 = h.pluviometer_id GROUP BY pid ORDER BY pid ASC ) AS p ON pid = pluviometers.id GROUP BY pluviometers.id ORDER BY pluviometers.id ASC 之类的声明中,是否可以知道哪一行匹配?如果是这样,怎么办呢?你会如何在PHP中捕获它?

1 个答案:

答案 0 :(得分:4)

使用Case/IF声明

SELECT *,
       case when row1='val1' then 'row1' else 'row2' end as matched col
FROM tbl1
WHERE row1='val1' OR row2='val2'

Case语句可以替换为IF语句,如此

IF(row1='val1','row1','row2')