在地方之间找到

时间:2019-03-09 10:12:11

标签: sql sql-server stored-procedures mysqli plsql

  1. 我有一张桌子tblroute_details

enter image description here

  1. 我想在地方之间寻找,我使用了该查询

    SELECT (placeName) 
      FROM tblRouteDetails
     WHERE ID Between (
            SELECT  ID 
              FROM tblRouteDetails 
              WHERE placeName = 'Kaushambi' ) 
         AND (SELECT ID 
              FROM tblRouteDetails 
              WHERE placeName = 'Ghaziabad') 
    

    按ID订购

出现错误

Msg 512, Level 16, State 1, Line 3
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

3 个答案:

答案 0 :(得分:0)

SELECT id, placeName FROM tblRouteDetails 
WHERE placeName in ('Kaushambi','Ghaziabad') 
ORDER BY ID

答案 1 :(得分:0)

子查询SELECT ID FROM tblRouteDetails WHERE placeName = 'Ghaziabad'SELECT ID FROM tblRouteDetails WHERE placeName = 'Kaushambi'将返回多个记录。 试试

SELECT ( placename )
FROM   tblroutedetails
WHERE  id BETWEEN (SELECT id
                   FROM   tblroutedetails
                   WHERE  placename = 'Kaushambi'
                   LIMIT  1) AND 
                   (SELECT id
                   FROM   tblroutedetails
                   WHERE  placename = 'Ghaziabad'
                   LIMIT  1)
ORDER  BY id  
  

好吧,我输入placeName Kaushambi和Ghaziabad我想要ID PlaceName 9   Kaushambi 10 Ghaziabad 28 Kaushambi 29 Ghaziabad

SELECT id, placeName FROM tblroutedetails
WHERE placeName RLIKE 'Kaushambi|Ghaziabad' 
ORDER BY id

答案 2 :(得分:0)

您有placeName = 'Kaushambi'placeName = 'Ghaziabad'多于1行,这就是为什么您会收到错误消息。
试试这个:

SELECT placeName 
FROM tblRouteDetails 
WHERE ID Between 
  (SELECT MIN(ID) FROM tblRouteDetails WHERE placeName = 'Kaushambi') 
  AND 
  (SELECT MAX(ID) FROM tblRouteDetails WHERE placeName = 'Ghaziabad') 
  AND placeName <> 'Kaushambi' AND placeName <> 'Ghaziabad'
ORDER BY ID