SQL 查询 - 仅当不存在精确匹配时才使用 Like?
我遇到了当前使用的查询问题
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我只能想到用代码来做。 查找完全匹配,如果结果为空,则查找 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.
使用 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.
你说的是短路评估。
看看这篇文章它可能对你有帮助:
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
听起来您想首先基于精确匹配来加入别名为 pwd 和 ewd 的表,如果失败,则根据您现在拥有的类似比较。
试试这个:
然后,在您的 select 子句中,使用类似这样的内容:
但是,如果您正在处理 pwd 中可以为空的字段,这会导致问题,但这应该可以工作:
您还必须确保进行分组依据,因为连接到 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:
Then, in your select clause, use something like this:
however, if you are dealing with a field that can be null in pwd, that will cause problems, this should work though:
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.