我为我的问题中缺乏信息而道歉。我正在使用MS SQL。我正在寻找文本字段中的3个特定项目; IH-,MPC或ICC。如果在文本字段中找到任何这些项目,我想表明它们已被找到。如果在第一行中找到所有三个,则会有一个包含IH-,MPC,ICC的字段 这是我的询问;
SELECT tt.TITLE, tt.ITEM_REFERENCE_ID, tt.VERSION, PATH_MAPPINGS.PATH, tt.CONTENT
FROM ITEM_REFERENCES INNER JOIN
PATH_MAPPINGS ON ITEM_REFERENCES.ORGANIZATIONAL_ITEM_ID = PATH_MAPPINGS.ORGANIZATIONAL_ITEM_ID INNER
JOIN
ITEMS AS tt INNER JOIN
(SELECT ITEM_REFERENCE_ID, MAX(VERSION) AS MaxVersion
FROM ITEMS
GROUP BY ITEM_REFERENCE_ID) AS groupedtt ON tt.ITEM_REFERENCE_ID = groupedtt.ITEM_REFERENCE_ID AND tt.VERSION = groupedtt.MaxVersion ON
ITEM_REFERENCES.ID = tt.ITEM_REFERENCE_ID
WHERE (tt.CONTENT LIKE '%<text>AP[0-2]%' )
or
(tt.CONTENT LIKE '%<text>IH-[0-2]%' )
or
(tt.CONTENT LIKE '%<text>MPC[0-2]%' )
or
(tt.CONTENT LIKE '%<text>ICC[0-2]%' )
答案 0 :(得分:0)
Select Case When FIELD Like '%BLUE%' Then 'BLUE'
Else When FIELD Like '%RED%' Then 'RED'
Else When FIELD Like '%AT%' Then 'AT' End
From Table
Where (FIELD Like '%BLUE%'
Or FIELD Like '%RED%'
Or FIELD Like '%AT%')
答案 1 :(得分:0)
Postgres的:
select
(blue like '%blue%') as found_blue,
(blue like '%red%') as found_red
from (select '***blue***'::text as blue) t;
found_blue │ found_red
────────────┼───────────
t │ f
(1 row)
MySQL的:
select
(blue like '%blue%') as found_blue,
(blue like '%red%') as found_red
from (select '***blue***' as blue) t;
+------------+-----------+
| found_blue | found_red |
+------------+-----------+
| 1 | 0 |
+------------+-----------+
1 row in set (0.02 sec)
答案 2 :(得分:0)
使用仅包含内容一个想法的简化数据表是创建一个包含搜索条件的临时表,以根据原始数据对其进行检查。
临时表可以是CTE
,如下面的查询
WITH ContentPattern AS (
SELECT '%blue%' c
UNION ALL
SELECT '%red%'
UNION ALL
SELECT '%at%'
UNION ALL
SELECT '%green%'
UNION ALL
SELECT '%orange%'
)
SELECT a.[field], f.Found
FROM Table1 a
CROSS APPLY (SELECT STUFF((SELECT ', ' + cp.c
FROM ContentPattern cp
WHERE PATINDEX(cp.c, a.[field]) > 0
GROUP BY cp.c
HAVING COUNT(cp.c) > 0
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
, 1, 1, '') Found) f
在问题中,模式具有可变部分,更改CTE
的值就足够了,但可以做更多的事情:获取实际值而不是搜索条件。要做到这一点,我们需要模式的长度,最简单的方法是...以某种方式预先计算它,并将其添加到临时表/ CTE
WITH ContentPattern AS (
SELECT '%blue[0-2]%' c, 5 l
UNION ALL
SELECT '%red[0-2]%', 4
UNION ALL
SELECT '%at[0-2]%', 3
UNION ALL
SELECT '%green%', 5
UNION ALL
SELECT '%orange%', 6
)
SELECT a.[field], f.Found
FROM Table1 a
CROSS APPLY (
SELECT STUFF((
SELECT ', '
+ SUBSTRING(a.[field], PATINDEX(cp.c, a.[field]), cp.l)
FROM ContentPattern cp
WHERE PATINDEX(cp.c, a.[field]) > 0
GROUP BY cp.c, cp.l
HAVING COUNT(cp.c) > 0
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
, 1, 1, '') Found) f
由于该字段位于查询中,即使所有内容都没有搜索到的模式,NULL
值,WHERE
条件LEN
条件,它也会立即显示在结果中Found
字段的1}}可以删除所有不匹配的行。