SQL 查询 - 仅当不存在精确匹配时才使用 Like?

发布于 2024-07-04 03:57:16 字数 295 浏览 10 评论 0原文

我遇到了当前使用的查询问题

LEFT JOIN weblog_data AS pwd 
  ON (pwd.field_id_41 != '' 
  AND pwd.field_id_41 LIKE CONCAT('%', ewd.field_id_32, '%'))

,但是我发现我需要它仅在首先没有完全匹配的情况下才使用它。 发生的情况是,由于使用 LIKE,查询是双重浸入的,因此如果它首先测试精确匹配,那么它将避免双重浸入问题。 有人可以为我提供任何进一步的指导吗?

I'm having an issue with a query that currently uses

LEFT JOIN weblog_data AS pwd 
  ON (pwd.field_id_41 != '' 
  AND pwd.field_id_41 LIKE CONCAT('%', ewd.field_id_32, '%'))

However I'm discovering that I need it to only use that if there is no exact match first. What's happening is that the query is double dipping due to the use of LIKE, so if it tests for an exact match first then it will avoid the double dipping issue. Can anyone provide me with any further guidance?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

红墙和绿瓦 2024-07-11 03:57:16

我只能想到用代码来做。 查找完全匹配,如果结果为空,则查找 LIKE。
另一种选项是此查询中的 WHERE,使得 WHERE ({精确匹配的计数}=0),在这种情况下,如果精确匹配返回超过 0 个结果,则不会与 LIKE 进行比较。 但它的效率非常低……更不用说在代码中有意义地使用它是相当困难的。

我会选择 If(精确匹配计数 = 0) 然后执行类似查询,否则只使用精确匹配的结果。

I can only think of doing it in code. Look for an exact match, if the result is empty, look for a LIKE.
One other option is a WHERE within this query such that WHERE ({count from exact match}=0), in which case, it wont go through the comparison with LIKE if the exact match returns more than 0 results. But its terribly inefficient... not to mention the fact that using it meaningfully in code is rather difficult.

i'd go for a If(count from exact match = 0) then do like query, else just use the result from exact match.

那些过往 2024-07-11 03:57:16

使用 TSQL,运行精确匹配,检查行数 == 0,如果是,则运行类似,否则不要运行类似或在精确匹配下方添加类似结果。

using TSQL, run an exact match, check for num of rows == 0, if so, run the like, otherwise don't run the like or add the like results below the exact matches.

离笑几人歌 2024-07-11 03:57:16

你说的是短路评估。

看看这篇文章它可能对你有帮助:
http://beingmarkcohen.com/?p=62

you're talking about short circuit evaluation.

Take a look at this article it might help you:
http://beingmarkcohen.com/?p=62

你在看孤独的风景 2024-07-11 03:57:16

听起来您想首先基于精确匹配来加入别名为 pwd 和 ewd 的表,如果失败,则根据您现在拥有的类似比较。

试试这个:

LEFT JOIN weblog_data AS pwd1 ON (pwd.field_id_41 != '' AND pwd.field_id_41 = ewd.field_id_32)
LEFT JOIN weblog_data AS pwd2 ON (pwd.field_id_41 != '' AND pwd.field_id_41 LIKE CONCAT('%', ewd.field_id_32, '%'))

然后,在您的 select 子句中,使用类似这样的内容:

select
  isnull(pwd1.field, pwd2.field)

但是,如果您正在处理 pwd 中可以为空的字段,这会导致问题,但这应该可以工作:

select
  case pwd1.nonnullfield is null then pwd2.field else pwd1.field end

您还必须确保进行分组依据,因为连接到 pwd2 仍会将行添加到结果集中,即使您最终忽略其中的数据。

It sounds like you want to join the tables aliased as pwd and ewd in your snippet based first on an exact match, and if that fails, then on the like comparison you have now.

Try this:

LEFT JOIN weblog_data AS pwd1 ON (pwd.field_id_41 != '' AND pwd.field_id_41 = ewd.field_id_32)
LEFT JOIN weblog_data AS pwd2 ON (pwd.field_id_41 != '' AND pwd.field_id_41 LIKE CONCAT('%', ewd.field_id_32, '%'))

Then, in your select clause, use something like this:

select
  isnull(pwd1.field, pwd2.field)

however, if you are dealing with a field that can be null in pwd, that will cause problems, this should work though:

select
  case pwd1.nonnullfield is null then pwd2.field else pwd1.field end

You'll also have to make sure to do a group by, as the join to pwd2 will still add rows to your result set, even if you end up ignoring the data in it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文