开发者

Which column matched?

开发者 https://www.devze.com 2023-02-08 03:10 出处:网络
How do I know which column matched if I have two LIKE clauses for two different columns and eventually one of the two columns contains 开发者_StackOverflow社区the needle. you could do something like:

How do I know which column matched if I have two LIKE clauses for two different columns and eventually one of the two columns contains 开发者_StackOverflow社区the needle.


you could do something like:

SELECT 1 AS `match`, * FROM `table` WHERE col1 LIKE 'needle%'
UNION 
SELECT 2 AS `match`, * FROM `table` WHERE col2 LIKE 'needle%'

and use match to know

Note: this could probably be written with a IF()


SELECT
  IF(col1 LIKE 'needle%' AND col2 LIKE 'needle%', 0, IF(col1 LIKE 'needle%', 1, 2)
    AS match, 
  *
FROM
  table
WHERE
  col1 LIKE 'needle%'
OR
  col2 LIKE 'needle%'

match teklls you which colums hit:

  • 0 = both
  • 1 = match on col1
  • 2 = match on col2


You can use your PHP section to check which one matched.

if (stristr($result['col1'], $queryvar) === true) // col1 matched
0

精彩评论

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