Krii
Krii

Reputation: 907

SQL find which OR was matched

In a statement like: SELECT * FROM tbl1 WHERE row1=val1 OR row2=val2, is it possible to know which row was matched? If so how could it be done? And how would you capture this in PHP?

Upvotes: 4

Views: 37

Answers (1)

Pரதீப்
Pரதீப்

Reputation: 93694

Use Case/IF statement

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

Case statement can be replaced with IF statement like this

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

Upvotes: 4

Related Questions