【问题标题】:Deriving all employees from table having department id starting with 250 and 270?从部门 id 以 250 和 270 开头的表中派生所有员工?
【发布时间】:2015-07-28 16:05:33
【问题描述】:

我正在使用 SQL plus 和 oracle 10.2.0.1.0。我想从具有更多字段的 ps_job 表中检索部门 (deptidvarchar2) 编号以“270”、“250”开头的员工。

这是我写的查询-

从 ps_job 中选择不同的部门 where deptid in ('250%', '270%');

它没有显示任何行,如果我删除不同的行,它也会给我很多结果,但结果也有重复。

【问题讨论】:

    标签: oracle10g sqlplus


    【解决方案1】:

    in 不接受通配符。这样做:

    select distinct deptid from ps_job 
    where (deptid like '250%'
           or deptid like '270%');
    

    或者:

    select distinct deptid from ps_job 
    where substr(deptid,1,3) in ('250', '270');
    

    虽然它不会在 deptid 上使用索引,而第一个版本可能(但可能不会)。

    【讨论】:

    • 为什么要区分 deptid 而不是employeeid? - 如果一个employeeid 是两个deptid 的一部分怎么办?
    • 我不知道 - 这不是我的问题!
    【解决方案2】:

    我做了什么,我将查询分解为 2 个不同的查询。 1 代表 250,其他代表 270,我使用 LIKE,之前使用的是 IN,但不知何故无法正常工作。

    从 ps_job 中选择不同的部门 其中 deptid LIKE '250%';

    从 ps_job 中选择不同的部门 其中 deptid LIKE '250%';

    Bob,就您而言,我们的数据库没有这样的规定,因为我们使用有效日期和顺序,所以它可以确保员工目前有 1 个部门 ID。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-09
      • 1970-01-01
      • 1970-01-01
      • 2020-04-05
      • 1970-01-01
      • 1970-01-01
      • 2016-08-26
      相关资源
      最近更新 更多