Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Which COLUMN qualified my Query? 2

Status
Not open for further replies.

JSProgramIA

Programmer
Oct 28, 2005
41
US
I am trying to figure out which column the query was qualified on.

For instance:
Code:
SELECT * 
  FROM products 
  WHERE product_id LIKE '%somethingHere%' 
        OR product_description LIKE '%somethingHere%'

And what I want to know is, which column qualified the query. Was it product_id, or product_description, or both? I am trying to use this in a search results page, and I would like to tell the user which column their keyword found a match on.

Is it possible to return a user defined column, such as matched_in and have a value in that column describe which column we made a match on?

Thanks for any help you may offer.
 
How about:
[tt]
SELECT
*,
product_id LIKE '%somethingHere%' idmatch,
product_description LIKE '%somethingHere%' descmatch
FROM products
WHERE
product_id LIKE '%somethingHere%'
OR product_description LIKE '%somethingHere%'
[/tt]
 
This appears to be returning a boolean value? Is that correct?

Anyways, this will work just fine. Could you explain the following code a bit more, please?

Code:
SELECT
  *,
  product_id LIKE '%somethingHere%' idmatch

Am I reading this right, and you can put conditional statements in the SELECT portion of the query????

Just when I thought I was getting this SQL thing...
 
product_id LIKE '%somethingHere%'" is an expression which evaluates to a boolean value of 0 or 1, regardless of where it's used.
 
regardless of where it's used" could perhaps be qualified -- this may be the case in mysql, but it would fall down in any other database

better to use standard sql, it's not really that much more work to write, it works just as well in mysql, and it is certainly a lot easier to understand

plus, you can assign your own true/false values (not all systems are consistent, sometimes 0=false, sometimes -1=true)
Code:
select *
     , case when product_id 
            like '%somethingHere%' 
            then 'Y' else ' ' 
         end   as idmatch
     , case when product_description 
            like '%somethingHere%' 
            then 'Y' else ' ' 
         end   as descmatch
  from ...

r937.com | rudy.ca
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top