簡體   English   中英

SQL的where子句中的select語句

[英]select statement in where clause sql

我的查詢有什么問題,我無法弄清楚。

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表中有一行名為“部門存儲”

查詢工作正常,直到

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+'%'

這條線怎么了?

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+'%'

嘗試這個

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”。 與刪除。 工作正常!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM