SQL从每个2个或更多个不同的表中选择1列

时间:2014-12-03 23:06:51

标签: mysql sql

我在mysql中有这些表:

students    time    class_student
someone     1   something01
someone     2   something02
someone     3   something03
someone     4   something04
someone     5   something05


theachers   time    class_teachers
someone     1   something11
someone     3   something12
someone     5   something13
someone     7   something14
someone     9   something15
someone     11  something16

我需要在两个表中始终按照每个表顺序获取类filds,比如

    time    class_student class_teachers
    1   something01         something11
    2   something02
    3   something03         something12
    4   something04
    5   something05         something13
    7                       something14
    9                       something15
    11                      something16

首先我测试这个查询结果在两个表中都是

SELECT time FROM table1
UNION
SELECT time FROM table2
ORDER BY time

我有这个查询返回数据,但在同一个fild

SELECT class_student FROM table1 WHERE time IN (
                                                    SELECT time FROM table2
                                                    UNION
                                                    SELECT time FROM table1
                                                    ORDER BY time
                                                    )
UNION
SELECT class_teachers FROM table2 WHERE time IN (
                                            SELECT time FROM table2
                                            UNION
                                            SELECT time FROM table1
                                            ORDER BY time
                                            );

我尝试了这个,但是sql没有执行它

SELECT
    class_student,
    class_teachers
FROM 
    table1
    inner join
    table2 on time IN (
                SELECT time FROM table1
                UNION
                SELECT time FROM table2
                ORDER BY time
                );

2 个答案:

答案 0 :(得分:1)

试试这个。

SELECT CASE WHEN c.time IS NULL THEN a.time  ELSE c.time END AS time, c.class_student, a.class_teachers
FROM table1 c
FULL JOIN table2 a ON c.time = a.time

答案 1 :(得分:0)

我找到了这个解决方案

SELECT 
CASE 
WHEN table1.time IS NOT NULL THEN table1.time
WHEN table2.time IS NOT NULL THEN table2.time
WHEN table3.time IS NOT NULL THEN table3.time
END AS time,
table1.valor as table1, table2.valor as table2 , table3.valor as table3
FROM table1
LEFT OUTER JOIN table2 USING (time)
LEFT OUTER JOIN table3 USING (time)
UNION
SELECT 
CASE 
WHEN table1.time IS NOT NULL THEN table1.time
WHEN table2.time IS NOT NULL THEN table2.time
WHEN table3.time IS NOT NULL THEN table3.time
END AS time,
table1.valor as table1, table2.valor as table2 , table3.valor as table3
FROM table2
LEFT OUTER JOIN table1 USING (time)
LEFT OUTER JOIN table3 USING (time)
UNION
SELECT 
CASE 
WHEN table1.time IS NOT NULL THEN table1.time
WHEN table2.time IS NOT NULL THEN table2.time
WHEN table3.time IS NOT NULL THEN table3.time
END AS time,
table1.valor as table1, table2.valor as table2 , table3.valor as table3
FROM table3
LEFT OUTER JOIN table1 USING (time)
LEFT OUTER JOIN table2 USING (time)
ORDER BY time;