开发者

Which column matched?

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜