在where子句sql中选择语句

时间:2014-03-05 06:14:15

标签: sql wildcard where-clause sql-like

我的查询有什么问题我无法解决。

Select id, name, description, road_st, sub_area, area, website, email, category,
           working_hrs, inside_building, logo, latitude, longitude, 
           geom.STNumPoints() as vertices, 
           geom.STAsText() as geom,house_no,building,shop
            from Points 
            where @hr.STIntersects(geog)= 1  
            and deleted=0
            and (select s.name from Sub_Category s where s.id=category) 
            like  '%'+@text+'%'

错误:
如果@text='store'

我没有得到任何结果......但SUb_category表中有一行名为" Departmental Store"

查询工作正常

Select id, name, description, road_st, sub_area, area, website, email, category,
           working_hrs, inside_building, logo, latitude, longitude, 
           geom.STNumPoints() as vertices, 
           geom.STAsText() as geom,house_no,building,shop
            from Points 
            where @hr.STIntersects(geog)= 1  
            and deleted=0

我添加此行时没有结果

            and (select s.name from Sub_Category s where s.id=category) 
            like  '%'+@text+'%'

这条线怎么了?

2 个答案:

答案 0 :(得分:1)

Select id, name, description, road_st, sub_area, area, website, email, category,
           working_hrs, inside_building, logo, latitude, longitude, 
           geom.STNumPoints() as vertices, 
           geom.STAsText() as geom,house_no,building,shop
            from Points 
            where @hr.STIntersects(geog)= '1'  
            and deleted='0'
            and (select s.name from Sub_Category s where s.id=category) 
            like  '%'+@text+'%'

试试这个

答案 1 :(得分:0)

SELECT 
            p.id, 
            p.name, 
            s.name as street, 
            sa.name as sub_area, 
            a.name as area, 
            c.name as city,
            p.website,
            p.email, 
            pr.name as province,
            p.description, 
            sc.name as category, 
            u.UserName as AddedBy, 
            u1.UserName as EditedBy, 
            p.date_added, p.date_edited, 
            p.latitude, 
            p.longitude, 
            p.working_hrs, 
            p.inside_building,
            p.shop as shop,
            p.geom.STNumPoints() as vertices, p.geom.STAsText() as geom,
            hn.name as house,(select abc.name from Points abc where abc.id=p.building)as building 
            from Points p inner join Road_Street s on p.road_st = s.id 
            inner join Subarea sa on p.sub_area = sa.id 
            inner join Area a on p.area = a.id
            inner join City c on a.city = c.id 
            inner join Sub_Category sc on p.category = sc.id 
            inner join aspnet_users u on p.added_by = u.UserId 
            inner join aspnet_users u1 on p.edited_by = u1.UserId 
            inner join Province pr on c.province = pr.id
            inner join House_Number hn on p.house_no = hn.id
            where @hr.STIntersects(p.geog)= 1 
            and p.deleted=0
            and (select stt.name from Sub_Category stt where stt.id=p.category) like  '%'+@text+'%'

描述/校正: 没有什么东西在查询中扭曲,错误是由于一个暧昧的列名而发生的....在第二行最后一行我添加了" p。"删除。它工作得很好!