简体   繁体   English

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

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

What is the best way to to perform this query. 执行此查询的最佳方法是什么。 I have the following table 我有下表

mytable with columns 列表的mytable

x y 
1 a
2 b
3 c

and I would like to (in pseudo sql) 我想(在伪sql中)

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

when 什么时候

x1 is x where y=a

x2 is x where y=b

x3 is x where y=c

so I would like as a result 所以我想结果

1, 2, 3

I am currently using cte's and and a very large dataset, I am trying to reduce the query time, is it always necessary to have 3 table scans ? 我目前正在使用cte和一个非常大的数据集,我试图减少查询时间,是否总是需要进行3次表扫描?

You should use 3 queries. 您应该使用3个查询。 It will be a lot faster with proper indexing when self joins. 在自连接时使用适当的索引会更快。 Additionally it will be more readable. 此外,它将更具可读性。

If you would like one query call, it might be this :) 如果你想要一个查询调用,它可能是这个:)

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

I would of done this : 我会这样做的:

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

Bit easier to understand, and pull out more info if you need multiple columns from each row 更容易理解,如果每行需要多列,请提取更多信息

In the example given, there are only 3 rows of input and one row of output. 在给出的示例中,只有3行输入和1行输出。 I assume that there is going to be at least one other column involved, such that input data: 我假设至少涉及一个其他列,例如输入数据:

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

is to become output: 是输出:

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

This can be done in a single pass using a query like: 这可以使用如下查询一次完成:

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

Another solution: 另一种方案:

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

You will have a result set: 你将得到一个结果集:

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

This result set can then be used in an application to get desired result. 然后可以在应用程序中使用此结果集来获得所需结果。

From your question it appears you would like the last three instances of a condition, or tie three different conditions together. 从你的问题看来,你想要一个条件的最后三个实例,或者将三个不同的条件联系在一起。 Would the following example satisfy your question: 以下示例是否满足您的问题:

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

Lets say we only want the students which have three specific courses (gen101, cmp202, and psy303) and show those grades ignoring anyone else. 让我们说我们只想要有三门特定课程(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'

This should give one row: 这应该给出一行:

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

Hope that gives you enough to work on. 希望能给你足够的工作。

SQL Fiddle SQL小提琴

MySQL 5.5.32 Schema Setup : 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')
;

Query 1 : 查询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 |
SELECT Case When y = 1 Then x1 When y = 2 Then x2 Else x3 End FROM mytable

My recommendation would be to select the results ordered or grouped by column y and the use that information to split the resultset into several lists for an application to process. 我的建议是选择按y列排序或分组的结果,并使用该信息将结果集拆分为多个列表以供应用程序处理。 If you want to do this only in the database I'm afraid multiple table scans (or joins) are necessary. 如果你只想在数据库中这样做,恐怕需要多次表扫描(或连接)。

Another fix is to migrate the information in column y to another table (with a foreign key reference), to be able to join more effectively to that. 另一个解决方法是将第y列中的信息迁移到另一个表(使用外键引用),以便能够更有效地加入该表。

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

相关问题 基本sql:在一个查询中多次从同一列中选择AVG()值,当每个想要的AVG()值使用不同的WHERE子句时 - basic sql : selecting AVG() values from the same column multiple times in one query, when each wanted AVG() value uses different WHERE clause 在WHERE子句中多次使用相同的列 - Using same column multiple times in WHERE clause 获取 SQL 中同一列上不同 WHERE 子句的多列 - Get multiple columns for different WHERE clause on the same column in SQL SQL:使用 3 个不同的 where 子句查询同一列 3 次 - SQL: Query the same column 3 times with 3 different where clauses SQL / Impala:将多个查询(具有不同的where子句)组合为一个 - SQL/Impala: combined multiple query (with different where clause) into one SQL多次选择一列 - SQL selecting one column multiple times SQL - 多个条件where子句相同的列 - SQL - Multiple conditions where clause the same column 使用“特殊”WHERE子句从一个SQL查询中的表中选择数据 - Selecting data from a table in one sql query with 'special' WHERE clause 我如何处理多次选择显示不同数据的SQL DB中的同一列? - How would I approached selecting the same column in a SQL DB multiple times displaying different data? 选择多列时,SQL查询返回值,但仅选择一列时,无数据返回 - SQL query returns values when selecting multiple columns, but no data when selecting only one column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM