简体   繁体   中英

Check which column matched on MySQL like query

Is it possible to find out which column was matched against on a multiple LIKE query?

eg:

SELECT * FROM table WHERE col_a LIKE = '%B' OR col_b LIKE = '%A'

if(MATCHED COL_A){
   do this;
}else{
   do that;
}
SELECT col_a, col_b FROM table WHERE col_a LIKE "%B" OR col_b LIKE "%A"

if (strpos($colA, $needle) !== false) {
// do this
} elseif (strpos($colB, $needle) !== false) {
// do that
}

You can simply put your conditions in select statement and name it with new alias which will have a boolean value 0/1 for matching columns,Also = after like doesn't make any sense

SELECT *,
col_a LIKE  '%B' AS match_cola,
col_b LIKE  '%A' AS match_colb
FROM table WHERE col_a LIKE '%B' OR col_b LIKE '%A'

Fetching result from query you can check it as

$result = fetch result from query;
//loop
if($result['match_cola'] == 1){
echo 'col_a matched';
}
//end loop

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM