如何在sql查询中传递多个城市id

时间:2016-08-26 09:16:20

标签: sql

我的SQL查询如下

select DISTINCT date_TripDate AS TripDate,
                traveltype.varchar_TravelTypeCode AS TripType,
                contactss.Value as Mobilenumber,
from [OneC_988].[dbo].[988_Details_VehicleRequest] request
join [OneC_988].[dbo].[988_Details_VehicleTrip] trip
    on  request.int_VehicleRequestID = trip.int_VehicleRequestID
    and int_CityID ='1'

当我按如下方式运行上述查询输出时

Tripdate TripType Mobilenumber int_CityID

26th aug 16 IN 9878778878 1
26th aug 16 IN 9949949990 1
26th aug 16 IN 9878778878 1

我想在上面的sql查询中传递INT_CityID 1和2以获得如下输出

Tripdate TripType Mobilenumber int_CityID

26th aug 16 IN 9878778878 1
26th aug 16 IN 9949949990 1
26th aug 16 IN 9878778878 1

28th aug 16 IN 9878778878 2
28th aug 16 IN 9949949990 2
28th aug 16 IN 9878778878 2

2 个答案:

答案 0 :(得分:1)

使用IN clauese,如下面的脚本..

select DISTINCT date_TripDate AS TripDate,traveltype.varchar_TravelTypeCode AS TripType,contactss.Value as Mobilenumber,from [OneC_988].[dbo].[988_Details_VehicleRequest] request join [OneC_988].[dbo].[988_Details_VehicleTrip] trip on request.int_VehicleRequestID=trip.int_VehicleRequestID
    and int_CityID  IN ('1','2')

答案 1 :(得分:0)

select DISTINCT 
    date_TripDate AS TripDate,
    traveltype.varchar_TravelTypeCode AS TripType,
    contactss.Value as Mobilenumber,
from 
    [OneC_988].[dbo].[988_Details_VehicleRequest] request 
join 
    [OneC_988].[dbo].[988_Details_VehicleTrip] trip 
on 
    request.int_VehicleRequestID=trip.int_VehicleRequestID
and 
    (int_CityID ='1' OR int_CityID ='2')

您可以在JOIN条件下使用OR。您也可以使用可能执行得更快的UNION。