繁体   English   中英

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

[英]basic sql : selecting the same column multiple times in one query, when each occurrence is dependent on different where clause

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

列表的mytable

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次表扫描?

您应该使用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

我会这样做的:

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行输入和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

另一种方案:

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

你将得到一个结果集:

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

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

从你的问题看来,你想要一个条件的最后三个实例,或者将三个不同的条件联系在一起。 以下示例是否满足您的问题:

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

希望能给你足够的工作。

SQL小提琴

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'

结果

| X1 | X2 | X3 |
|----|----|----|
|  1 |  2 |  3 |
SELECT Case When y = 1 Then x1 When y = 2 Then x2 Else x3 End FROM mytable

我的建议是选择按y列排序或分组的结果,并使用该信息将结果集拆分为多个列表以供应用程序处理。 如果你只想在数据库中这样做,恐怕需要多次表扫描(或连接)。

另一个解决方法是将第y列中的信息迁移到另一个表(使用外键引用),以便能够更有效地加入该表。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM