代码之家  ›  专栏  ›  技术社区  ›  NimChimpsky

基本sql:在一个查询中多次选择同一列,每次出现都依赖于不同的where子句

sql
  •  5
  • NimChimpsky  · 技术社区  · 15 年前

    执行此查询的最佳方法是什么。我有下表

    x y 
    1 a
    2 b
    3 c
    

    我想(在伪sql中)

    select x as x1 ,x as x2, x as x3 from mytable where ????
    

    x1 is x where y=a
    
    x2 is x where y=b
    
    x3 is x where y=c
    

    所以我希望

    1, 2, 3
    

    我目前正在使用cte和一个非常大的数据集,我试图减少查询时间,是否总是有必要有3个表扫描?

    8 回复  |  直到 15 年前
        1
  •  14
  •   Silver Light    15 年前

    您应该使用3个查询。当自己加入时,索引会更快。此外,它将更可读。

    如果您想要一个查询调用,可能是这样:)

    SELECT
    (SELECT x FROM table WHERE y=1) AS x1,
    (SELECT x FROM table WHERE y=2) AS x2,
    (SELECT x FROM table WHERE y=3) AS x3
    
        2
  •  5
  •   aqm    12 年前

    我愿意这样做:

    SELECT
        tableRowA.x as x1
        tableRowB.x as x2
        tableRowC.x as x3
    FROM
        table as tableRowA,
        table as tableRowB,
        table as tableRowC
    WHERE
        tableRowA.y = 1
        tableRowB.y = 2
        tableRowC.y = 3
    

        3
  •  3
  •   user359040 user359040    15 年前

    在给定的示例中,只有3行输入和1行输出。我假设至少还有一列涉及,这样输入数据:

    w  x  y
    ---------
    w1 1  a
    w1 2  b
    w1 3  c
    w2 4  a
    w2 5  b
    w2 6  c
    .
    .
    .
    

    将成为输出:

    w  x1 x2 x3
    -----------
    w1 1  2  3
    w2 4  5  6
    .
    .
    .
    

    select w,
           max(case when y = 'a' then x end) x1,
           max(case when y = 'b' then x end) x2,
           max(case when y = 'c' then x end) x3
    from datatable
    where y in ('a','b','c')
    group by w
    
        4
  •  1
  •   Silver Light    15 年前

    另一个解决方案:

    SELECT x, y FROM table WHERE y IN ('a', 'b')
    

    x | y
    -----
    1 | a
    2 | b
    

    然后,可以在应用程序中使用此结果集以获得所需的结果。

        5
  •  1
  •   PatrickP61    15 年前

    mytable:
    (unique keys 1..n)      (col1)  
    student-id | course-id | grade
    s1           gen101      g1
    s1           cmp202      g2
    s1           psy303      g3
    s1           c4          g4
    s2           c1          g5
    

    假设我们只想要有三门特定课程(gen101、cmp202和psy303)的学生,并且显示那些忽略其他人的成绩。

    select gen.student-id  as student-id
         , gen.grade       as gen101-gr
         , cmp.grade       as cmp202-gr
         , psy.grade       as psy303-gr
      from mytable  gen
         , mytable  cmp
         , mytable  psy
     where gen.course-id    = 'gen101'
       and gen.student-id   = cmp.student-id
       and cmp.course-id    = 'cmp202'
       and cmp.studnet-id   = psy.student-id
       and psy.course-id    = 'psy303'
    

    这应该给出一行:

    student-id  gen101-gr cmp202-gr psy303-gr
    s1          g1        g2        g3
    

        6
  •  0
  •   dotariel    15 年前
    SELECT Case When y = 1 Then x1 When y = 2 Then x2 Else x3 End FROM mytable
    
        7
  •  0
  •   Kalle    15 年前

    我的建议是选择按y列排序或分组的结果,并使用这些信息将结果集拆分为多个列表,以便应用程序进行处理。如果只想在数据库中执行此操作,恐怕需要多个表扫描(或联接)。

    另一个修复方法是将y列中的信息迁移到另一个表(带有外键引用),以便能够更有效地连接到该表。

        8
  •  0
  •   Fabien TheSolution    11 年前

    SQL Fiddle

    MySQL 5.5.32模式设置

    CREATE TABLE Table1
        (`x` int, `y` varchar(1))
    ;
    
    INSERT INTO Table1
        (`x`, `y`)
    VALUES
        (1, 'a'),
        (2, 'b'),
        (3, 'c')
    ;
    
    CREATE TABLE mytable
        (`x` int, `y` varchar(1))
    ;
    
    INSERT INTO mytable
        (`x`, `y`)
    VALUES
        (1, 'a'),
        (2, 'b'),
        (3, 'c')
    ;
    

    问题1 :

    SELECT x1.x as x1, x2.x as x2, x3.x as x3
    FROM mytable x1
    INNER JOIN mytable x2 ON x2.y='b'
    INNER JOIN mytable x3 ON x3.y='c'
    WHERE x1.y='a'
    

    Results :

    | X1 | X2 | X3 |
    |----|----|----|
    |  1 |  2 |  3 |