如何从表2中的一个表中找到特定的单词

时间:2017-08-09 10:37:39

标签: sql sql-server

table_name1:citymaster

id cityname
1  Florida
2  Newyork
3  Hydrabad

table_name2:详细信息

id detail
1  I am from washington
2  Newyork is beautiful
3  It is said that, Florida is very beautiful

table_name3:Cityfromdetails

(Which is the output table)
id City    
1  not available
2  newyork
3  Florida

我希望第三个表应该包含来自表详细信息的onlt城市名称。如果CItymaster中没有城市,那么输出应该不可用'

1 个答案:

答案 0 :(得分:2)

使用类似

的左连接
select d1.id, coalesce(c2.cityname, 'Not Available') as City
from details d1
left join citymaster c2
  on d1.detail like '%' + c2.cityname + '%'

或者我们可以使用SQL Server的CHARINDEX()函数:

select d1.id, coalesce(c2.cityname, 'Not Available') as City
from details d1
left join citymaster c2
    on charindex(c2.cityname, d1.detail) > 0;

http://momentjs.com/docs/#/displaying/format/