我认为这个查询应该可以工作,尽管我没有在您的示例数据之外对其进行测试。
另外,你没有指定你正在使用什么数据库,我只是在 MS SQL 上尝试过,但它应该很容易适应其他数据库,因为它只依赖于 charindex 和 left(或 substring)以及那些函数应该在大多数数据库上都可用。
SQL Fiddle
MS SQL Server 2008 架构设置:
create table your_table (article varchar(10), tag varchar (20))
insert your_table values
('Article 1','normal'),
('Article 2','apple'),
('Article 3','norm*'),
('Article 4','normalization'),
('Article 5','corvette')
查询 1:
declare @str varchar(30) = 'normalization'
select t.article, tag
from your_table t
left join (
select
article,
left(tag, charindex('*', tag,0)-1) t,
charindex('*', tag,0)-1 as l
from your_table
where charindex('*', tag,0) > 0
) a
on t.article = a.article
where (tag = @str) or (left(@str, l) = t)
Results:
| ARTICLE | TAG |
|-----------|---------------|
| Article 3 | norm* |
| Article 4 | normalization |
查询 2:
declare @str varchar(30) = 'normal'
select t.article, tag
from your_table t
left join (
select
article,
left(tag, charindex('*', tag,0)-1) t,
charindex('*', tag,0)-1 as l
from your_table
where charindex('*', tag,0) > 0
) a
on t.article = a.article
where (tag = @str) or (left(@str, l) = t)
Results:
| ARTICLE | TAG |
|-----------|--------|
| Article 1 | normal |
| Article 3 | norm* |