我想查询一个数据库,看看一个URL是否包含一个字符串,并让它返回唯一标识符url,并且还有我正在搜索的字符串的另一列。
因此,如果初始查询是:
select c.id as id , c.url as url, 'string' as string
from database c
where c.url like '%hello%'
我希望它返回
+-------+-------------------+--------+
| id | url | string |
+-------+-------------------+--------+
| 12345 | www.abc.com/hello | hello |
+-------+-------------------+--------+
任何帮助将不胜感激!
答案 0 :(得分:0)
如果您使用MySQL,这可以帮助您:
select c.id as id , c.url as url, SUBSTRING_INDEX(c.url,'/',-1) as string
from database c
where c.url like '%hello%'
答案 1 :(得分:0)
您没有说明您的DBMS,因此这是ANSI SQL:
select c.id, c.url, x.val as string
from database c
join (values ('hello')) as x(val) on c.url like '%'||x.val||'%'
请注意,不需要简单地重复列名称的别名c.id as id
与c.id