如何知道满足了多少个OR条件?

发布于 2024-09-11 06:06:48 字数 236 浏览 2 评论 0原文

我如何知道我的哪个结果满足了多少个条件/

SELECT 
    [TITLE] 
FROM 
    [M_TIPS] 
WHERE 
    [TITLE] LIKE '%VALUE%' OR 
    [TITLE] LIKE '%SQL%'; 

如何知道哪个结果仅满足 1 个 OR 条件以及哪个结果满足两个条件。条件的数量不是静态的,它可以增加。

有人可以帮忙吗?

How can i know that which of my result has satisfied how many number of conditions/

SELECT 
    [TITLE] 
FROM 
    [M_TIPS] 
WHERE 
    [TITLE] LIKE '%VALUE%' OR 
    [TITLE] LIKE '%SQL%'; 

How to know that which of the result has satisfied only 1 OR condition and which of the result has satisfied both conditions. The number of conditions are not static, it can increase.

Can anyone please help ?

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

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

发布评论

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

评论(3

潇烟暮雨 2024-09-18 06:06:48

将其添加到结果中:

SELECT 
    [TITLE] 
    case [TITLE] LIKE '%VALUE%' WHEN true then 1 else 0 end as CONTAINS_VALUE
    case [TITLE] LIKE '%SQL%' WHEN true then 1 else 0 end as CONTAINS_SQL
FROM 
    [M_TIPS] 
WHERE 
    [TITLE] LIKE '%VALUE%' OR 
    [TITLE] LIKE '%SQL%'; 

Add that to the result:

SELECT 
    [TITLE] 
    case [TITLE] LIKE '%VALUE%' WHEN true then 1 else 0 end as CONTAINS_VALUE
    case [TITLE] LIKE '%SQL%' WHEN true then 1 else 0 end as CONTAINS_SQL
FROM 
    [M_TIPS] 
WHERE 
    [TITLE] LIKE '%VALUE%' OR 
    [TITLE] LIKE '%SQL%'; 
秋叶绚丽 2024-09-18 06:06:48

如果使用 OR,WHERE 子句中的短路*可能会阻止任何匹配计数超过 1。

将匹配列表存储在表变量中然后对匹配进行计数怎么样?

DECLARE @MATCH TABLE (SCORE INT, TOKEN VARCHAR(16))
INSERT @MATCH 
      SELECT 1, 'VALUE'
UNION SELECT 1, 'SQL'
UNION SELECT 1, 'CAKE'

SELECT 
    [TITLE], SUM(M.SCORE)
FROM 
    [M_TIPS] T INNER JOIN @MATCH M ON T.[TITLE] LIKE '%' + M.TOKEN + '%'
GROUP BY T.[TITLE]

==
sql             1
value           1
xx sql value xx 2

Short circuiting in a WHERE clause *may prevent any match count exceeding 1 if you use OR.

How about storing your match list in a table variable then counting matches;

DECLARE @MATCH TABLE (SCORE INT, TOKEN VARCHAR(16))
INSERT @MATCH 
      SELECT 1, 'VALUE'
UNION SELECT 1, 'SQL'
UNION SELECT 1, 'CAKE'

SELECT 
    [TITLE], SUM(M.SCORE)
FROM 
    [M_TIPS] T INNER JOIN @MATCH M ON T.[TITLE] LIKE '%' + M.TOKEN + '%'
GROUP BY T.[TITLE]

==
sql             1
value           1
xx sql value xx 2
看透却不说透 2024-09-18 06:06:48

假设您使用编程语言根据需要生成条件来动态生成查询,那么我建议像这样生成查询:

SELECT [TITLE], SUM([CONDITION_COUNT]) FROM
(SELECT [TITLE], 1 [CONDITION_COUNT] FROM [M_TIPS] 
    WHERE [TITLE] LIKE ConditionString1 UNION ALL
SELECT [TITLE], 1 [CONDITION_COUNT] FROM [M_TIPS] 
    WHERE [TITLE] LIKE ConditionString2 UNION ALL
...
SELECT [TITLE], 1 [CONDITION_COUNT] FROM [M_TIPS] 
    WHERE [TITLE] LIKE ConditionStringN) SQ
GROUP BY [TITLE]

Assuming you're using a programming language to dynamically generate the queries with the conditions generated as required, then I suggest generating the query like so:

SELECT [TITLE], SUM([CONDITION_COUNT]) FROM
(SELECT [TITLE], 1 [CONDITION_COUNT] FROM [M_TIPS] 
    WHERE [TITLE] LIKE ConditionString1 UNION ALL
SELECT [TITLE], 1 [CONDITION_COUNT] FROM [M_TIPS] 
    WHERE [TITLE] LIKE ConditionString2 UNION ALL
...
SELECT [TITLE], 1 [CONDITION_COUNT] FROM [M_TIPS] 
    WHERE [TITLE] LIKE ConditionStringN) SQ
GROUP BY [TITLE]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文