简体   繁体   English

SQL从搜索结果的开头排除“ The”

[英]SQL Exclude 'The ' from the beginning of a search result

I am trying to exclude 'A ' and 'The ' from the beginning of values in a column. 我试图从一列中的值的开头排除'A'和'The'。 I am searching for artist starting with the letter 'A' like: 我正在搜索以字母“ A”开头的艺术家,例如:

 AND artist LIKE LOWER('A%') AND artist NOT LIKE LOWER('A %')

Problem is this will give me artists starting with 'A%', and not giving me artists start with 'A %', but it will also exclude artists starting with 'AA%' 问题在于,这将使我的艺术家以“ A%”开头,而不是给我以“ A%”开头,但也会排除以“ AA%”开头的艺术家

If some band decided to name themselves 'A Apple', they would be excluded from my search. 如果某个乐队决定将自己命名为“ A Apple”,他们将被排除在我的搜索范围之外。 How could i do this query without excluding 'AA%' 我如何在不排除'AA%'的情况下进行此查询

Thanks in Advance! 提前致谢! Jay 周杰伦

AND artist LIKE ('A%')
AND (artist NOT LIKE LOWER('A %') OR artist LIKE ('A A%'))

I hope this is for a hobby or small db, because the LIKE statement won't be very friendly to performance. 我希望这是一个爱好或小型数据库,因为LIKE语句对性能不是很友好。

This is a common and cumbersome problem, often solved by having a second field which contains the artist name in the form "Rolling Stones, The" alongside the displayed name "The Rolling Stones". 这是一个常见且麻烦的问题,通常可以通过使用第二个字段来解决,该字段包含以“ Rolling Stones,The”形式出现的艺术家姓名以及显示的名称“ The Rolling Stones”。 Searches are performed against the former, but results displayed using the latter. 针对前者执行搜索,但使用后者显示结果。

But if you're stuck with the data you've got, you need something like: 但是,如果您坚持使用所获得的数据,则需要执行以下操作:

AND UPPER(artist) LIKE 'A%'
AND ( UPPER(artist) LIKE 'A A%' OR NOT (UPPER(artist) LIKE 'A %') )

If you have control over the database structure, and particularly if this is a large database, it may be more beneficial to simply add a "SearchArtist" column which contains the artist's name as it would appear in searches or be used for sorting. 如果您可以控制数据库的结构,特别是如果这是一个大型数据库,则简单地添加一个“ SearchArtist”列,其中包含艺术家的名称(可能会出现在搜索中或用于排序)可能会更有益。 This way you could display the full name but sort by the searchable name: 这样,您可以显示全名,但按可搜索名称排序:

select artist form table order by searchArtist; 通过searchArtist选择艺术家表格的顺序;

That way The Rolling Stones would appear in the list ahead of Steppenwolf... 这样,滚石乐队便会出现在Steppenwolf之前的列表中...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM