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?
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?
也许是这样的:
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 告诉你哪个是哪个。
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 注入
select *,
case when record_id = $var then 1 else 2 end as matches
from table
where record_id = $var or form_id = $var