0

I need some advice on how to accomplish this. If I have a query like SELECT * FROM catalog WHERE record_id = $var OR form_id = $var;

What I want to do is to know which column matched. Was it the record or form?

4

3 回答 3

4

也许是这样的:

select a,b,c, 1 from catalog where record_id = $var
union
select a,b,c, 2 from catalog where form_id = $var

然后 1 或 2 告诉你哪个是哪个。

于 2012-05-02T20:55:06.443 回答
1
SELECT *, 
CASE
    WHEN record_id = $var and form_id = $var THEN 'both'
    WHEN record_id = $var THEN 'record_id'
    WHEN form_id = $var   THEN 'form_id'
    ELSE 'none'     -- can't happen becouse of where clause
END CASE  as how_it_was
FROM catalog WHERE record_id = $var OR form_id = $var;

请注意 $var 上的 sql 注入

于 2012-05-02T21:08:00.497 回答
1
select *,
case when record_id = $var then 1 else 2 end as matches 
from table
where record_id = $var or form_id = $var
于 2012-05-02T21:08:45.777 回答