【问题标题】:Select statement based on multiple table sql基于多表sql的select语句
【发布时间】:2021-12-05 06:46:46
【问题描述】:

您好,我有下表

司机

| Id       | vehicleId     | Power         |
| -------- | --------------|-------------- |
| 123      | 2221          |112            |
| 124      | 2222          |111            |

车辆

 vehicleId| VehicleAvl|
| -------- | -------------- |
| 2221| Yes|
| 2222| Yes|

车辆国家

Id| vehicleId| VehicleField| VehicleValue |
| -------| -------- | -------------- | -------------- |
| 1 | 2221| Residence Country| USA|
| 2| 2221| Registered Country| USA|
| 3 | 2221| Available Country| CAN|
| 4| 2222| Residence Country| USA|

从上面我需要编写一个简单的选择查询,它将获取车辆所在国家/地区是否属于美国

我不需要返回任何列而不是简单的 Select 1

我只有 Driver 表 ID 作为输入,所以我该如何编写需求查询

SELECT 1 
  FROM Driver IES, Vehicle d1,
  
 
where     
 IES.id= :GID and d1.vehicleId= IES.vehicleId
 And (SELECT vehiclevalue AS RESIDENCE_COUNTRY
    FROM  VehicleCountry t2
    where t2.vehicleId=d1.vehicleId
    AND t2.vehiclefield= 'RESIDENCE COUNTRY'
    ) DR 
  AND (IES.POWER IS NOT NULL
  OR DR.RESIDENCE_COUNTRY   != 'USA'
  OR DR.RESIDENCE_COUNTRY   IS NULL)
 

【问题讨论】:

  • 根据您的要求,您只需要 VehicleCountry 表。您可以根据您的条件执行 select 1 in case 语句。
  • 不,我需要三个表的组合,因为我正在检查属于第一个和第二个表的其他字段。 ex AND (IES.POWER IS NOT NULL) 与第一张桌子有关,我手里只有第一张桌子的 ID

标签: sql oracle oracle11g


【解决方案1】:

这样的?请注意

  • 您发布的示例数据格式不正确,所以我不太明白 vehiclecountry 表中哪个值属于哪个列
  • 在您的示例中,由于两位司机的居住国美国,我将最后一个值修改为 UK,只是为了有所作为
  • #1 - 15 行中的样本数据;查询从第 17 行开始

SQL> with
  2  driver (id, vehicleid) as
  3    (select 123, 2221 from dual union all
  4     select 124, 2222 from dual
  5    ),
  6  vehicle (vehicleid, vehicleavl) as
  7    (select 2221, 'yes' from dual union all
  8     select 2222, 'yes' from dual
  9    ),
 10  vehiclecountry (id, vehicleid, vehiclefield, vehiclevalue) as
 11    (select 1, 2221, 'Residence Country' , 'USA' from dual union all
 12     select 2, 2221, 'Registered Country', 'USA' from dual union all
 13     select 3, 2221, 'Available Country' , 'USA' from dual union all
 14     select 4, 2222, 'Residence Country' , 'UK'  from dual
 15    )
 16  --
 17  select nvl(max(1), 0) result
 18  from driver d join vehicle v on v.vehicleid = d.vehicleid
 19  join vehiclecountry c on c.vehicleid = v.vehicleid
 20  where c.vehiclefield = 'Residence Country'
 21    and c.vehiclevalue = 'USA'
 22    --
 23    and d.id = &par_driver_id;
Enter value for par_driver_id: 123

    RESULT
----------
         1

SQL> /
Enter value for par_driver_id: 124

    RESULT
----------
         0

SQL>

【讨论】:

    【解决方案2】:

    显示给定司机的车辆居住国:

    select d.vehicleId, v.vehicleValue as residence_country
    from driver d
    join vehicleCountry vc on vc.vehicleId = d.vehicleId
      and vc.vehicleField = 'Residence Country'
    where d.id = $id
    

    相同,但仅限居住国家/地区为美国的车辆(因此无需显示国家/地区):

    select d.vehicleId
    from driver d
    join vehicleCountry vc on vc.vehicleId = d.vehicleId
      and vc.vehicleField = 'Residence Country'
      and vc.vehicleValue = 'USA'
    where d.id = $id
    

    【讨论】:

      【解决方案3】:
      SELECT 
        CASE vc.VehicleValue = 'USA' THEN 1 ELSE 0 END
      FROM driver d
      join vehicleCountry vc on vc.vehicleId = d.vehicleId
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-12-27
        • 2013-02-11
        • 1970-01-01
        • 2021-01-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多