开发者

Grabbing the matched context from MySQL LIKE query

开发者 https://www.devze.com 2023-01-23 11:49 出处:网络
Here\'s what the SQL code I\'m using thus far looks like: SELECT credentials.credential_id, clients.client_id,

Here's what the SQL code I'm using thus far looks like:

SELECT

    credentials.credential_id,
    clients.client_id,
    clients.client_name,
    services.service_id,
    services.service_name

FROM

    credentials

INNER JOIN

    clients ON clients.client_id = credentials.client_id

INNER JOIN

    services ON services.service_id = credentials.service_id

WHERE 

    services.service_name LIKE ?
    OR clients.client_name LIKE ?

As you can see, a record could be returned if either the service_name or client_name are matched to the value of my LIKE argument. This works well. Here are my questions:

1) While still providing a keyword search, how can I optimize this query to be as scalable as possible?

2) I would like MySQL to return the context for what was matched. In other words, I would like to be able to show the user why a particular record was returned by what the query matched. As you can tell, it could be either the service or client name, but when I expand this query, it could even be other items in another table. Does anyone hav开发者_StackOverflowe any resources to point me to regarding providing matched context in your MySQL queries?

Thanks for any advice.


Add the LIKE expressions to the field list.

SELECT ...
  services.service_name LIKE ? AS service_matched,
  clients.client_name LIKE ? AS client_matched
FROM
   ...
0

精彩评论

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