Which column matches?

How do I know which column matches if I have two sentences LIKEfor two different columns, and in the end one of the two columns contains a needle.

+3
source share
3 answers

you can 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 matchto know

Note: this can probably be written using IF()

+1
source
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 that the columns fall into:

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

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

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

Source: https://habr.com/ru/post/1788912/


All Articles