2016-03-18 51 views
0

我有叫Postgres数据库表结束:web_dataPostgres的SELECT语句where字段值与

我目前运行下面的SQL SELECT语句:

SELECT url_path FROM web_data WHERE url_path IS NOT NULL 

下面是一些例子的结果我会得到对于url_path:

/ 
/aboutus.html 
/services.php 
/images/twitter_counter.png 
/images/facebook_counter.png 
/mobile/windows/TileTemplate.xml 
/en_US/fbevents.js 
/css/style.css 

我想返回结果,其中url_path的终值是下列之一:

/ 
.html 
.htm 
.php 
.asp 
.aspx 
.pdf 

有没有办法将它放到SQL语句中,而不是获得所有结果,然后通过类似PHP的方式来处理它?

回答

2

使用LIKE操作:

select url_path 
from web_data 
where url_path like '%.htm' 
    or url_path like '%.html' 
    or url_path like '%.php' 
    or url_path like '%.asp' 
    or url_path like '%.aspx' 
    or url_path like '%.pdf' 
    or url_path like '%/'; 

http://www.postgresql.org/docs/current/static/functions-matching.html#FUNCTIONS-LIKE

+0

因此具有唯一%在开始意味着它这样结束了吗?因为使用LIKE语法我通常会放LIKE%.html%另外,如果我有很多记录,那么LIKE函数非常慢? – Ahmed

+0

@Ahmed:'%'是一个通配符'%.html'意味着任何以'.html'结尾的值。是的,这将会很慢,因为数据库不能使用regluar索引。如果你真的有那么多行,你需要创建一个[trigram](http://www.postgresql.org/docs/current/static/pgtrgm.html)索引,以便索引能够帮助你 –