How to find out if a line turned out through where where in sql / php

I do not know if this is possible:

SELECT * FROM stuff WHERE barcode = '123123' OR product like '%123123%' 

Suppose this returns 10 rows. Two out of 10 lines were returned because they had 123123 in the barcode.

Now it is possible to somehow make an alias / variable to know that it is coming, because it matches the barcode.

So what can I do:

 if($MatchWasFromBarcode) {.... } 

Or do I need to make 2 requests for this?

+4
source share
2 answers
 SELECT case when barcode='your_value' then 1 else 0 end as matches_bar_code , col1 , col2 , col3 , col4 FROM stuff WHERE barcode = '123123' OR product like '%123123%' 

Do not execute select * . Bad practice. List the columns you need.

+1
source

Assuming you are using MySQL:

 SELECT barcode = '123123' AS MatchWasFromBarcode, col1, col2, ..., coln FROM stuff WHERE barcode = '123123' OR product like '%123123%' 

For other databases, change the second row to the following:

  CASE WHEN barcode = '123123' THEN 1 ELSE 0 END AS MatchWasFromBarcode, 
+4
source

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


All Articles