大家好我使用的是Oracle 11.要求的是只选择我们拥有“纽约”和“达拉斯”的记录。请注意,具有相同记录的城市的顺序可能会更改。因此,从以下记录集中我应该只有两个记录(即Rec#3和5)。请注意,使用wm_concat函数
连接City中的值Rec# City
1 New York
2 Huston
3 New York;Dallas
4 Los Angles
5 Los Vegas;Dallas;San Dieago;New York
6 Huston;Chicago;Salt Lake City;Cleaveland
答案 0 :(得分:2)
我是在Postgres上写的,因为oracle小提琴是错的,但应该是相同的
<强> SQL Fiddle Demo 强>
SELECT *
FROM cities
WHERE "City" LIKE '%New York%'
AND "City" LIKE '%Dallas%'
问题是,如果您尝试搜索'York'
可以部分匹配的内容,并且还会找到'New York'
,那么您可以像这样修复
SELECT *
FROM cities
WHERE "City" LIKE '%York%'; -- will find `New York`
SELECT *
FROM cities
WHERE ';' || "City" || ';' LIKE '%;York%;'; -- correct solve
正如@Egor建议你可以用一个LIKE
SELECT *
FROM cities
WHERE ';' || "City" || ';;' || "City" || ';' LIKE '%;Dallas;%;New York;%' ;
答案 1 :(得分:0)
我不知道Oracle语法,但是这样:
Select columns where cities like "Dallas" and cities like "New York"
答案 2 :(得分:0)
试试这个
SELECT * from TABLE_NAME where City like '%New York%Dallas%' or City like '%Dallas%New York%'
答案 3 :(得分:0)
尝试使用REGEXP_LIKE,其中所寻求的城市也可以在线的起点或终点。正则表达式意味着所寻找的城市在行或分隔符的开头处进行,然后是分隔符或行的末尾。
with tbl(rec, city) as (
select 1, 'New York' from dual union
select 2, 'Huston' from dual union
select 3, 'New York;Dallas' from dual union
select 4, 'Los Angles' from dual union
select 5, 'Los Vegas;Dallas;San Dieago;New York' from dual union
select 6, 'Huston;Chicago;Salt Lake City;Cleaveland' from dual union
select 7, 'Huston;Chicago;Salt Lake City;New York;York' from dual union
select 8, 'York;Huston;Chicago;Salt Lake City' from dual
)
select rec, city
from tbl
where regexp_like(city, '(^|;)York(;|$)')
and regexp_like(city, '(^|;)New York(;|$)');