根据查询值更改伪列

时间:2019-05-03 14:22:31

标签: mysql sql

我试图运行查询并根据内部查询是否返回任何内容来更改伪列值。

基本上,我希望下面的代码能够将wi更改为1(如果id在table2中,将SI更改为1,如果id在table3中)

SELECT name, grade, id, 0 as WI, 0 as SI FROM students
WHERE id in table1
AND (id in table2
OR id in table3)

我一直在四处搜寻,但没有碰到任何东西,我认为主要是因为我不知道“假”列的实际名称。

任何帮助将不胜感激,谢谢!

编辑: 数据看起来像这样:

name  grade  id
s1    1      0
s2    1      1
s3    2      7

并且目标是在查询后出现类似这样的内容:

name  grade  id  WI  SI
s1    1      0   0   1
s2    1      1   1   1
s3    2      7   1   0

编辑2: 这是我的实际查询:

SELECT CARE1.students.first_name, CARE1.students.grade, CARE1.students.id, 
CARE1.student_test_info.test_num, CARE1.student_test_info.date_of_testing,
0 as WI, 0 as SI, 0 as OC, 0 as SL 
FROM CARE1.students INNER JOIN CARE1.student_test_info ON CARE1.students.id = CARE1.student_test_info.student_id

WHERE id IN (SELECT DISTINCT studentID from CARE1.WI)
AND (id NOT IN (SELECT DISTINCT studentID from CARE1.SI)
OR id NOT IN (SELECT DISTINCT studentID from CARE1.OC))

基本上每个表(WI,SI等)中都有测试信息,我们正在努力确保每个人都参加了所有测试。该查询的作用是向我提供错过测试的人员列表,但不会告诉我他们错过了哪些测试,这就是我要找出的原因。

4 个答案:

答案 0 :(得分:0)

SELECT name, grade, id, 1 as WI, 0 as SI FROM students
WHERE id in table1
AND id in table2
UNION
SELECT name, grade, id, 0 as WI, 1 as SI FROM students
WHERE id in table1 AND
id in table3

答案 1 :(得分:0)

INNER JOIN到table1以获取仅存在于此的ID。左移至表2和表3。如果需要除0或1以外的任何内容,使用case语句将使您可以灵活地基于此逻辑显示任何自定义值。

SELECT name, 
        grade, 
        id, 
        CASE WHEN t2.ID IS NOT NULL THEN 1 ELSE 0 END as WI --update with any value you want to display, 
        CASE WHEN t3.ID IS NOT NULL THEN 1 ELSE 0 END as SI 
    FROM 
        students s INNER JOIN
        table1 t1 on s.id = t1.id LEFT OUTER JOIN
        table2 t2 on s.id = t2.id LEFT OUTER JOIN
        table3 t3 on s.id = t3.id

答案 2 :(得分:0)

SELECT s.name, s.grade, s.id,
 MAX(t2.id IS NOT NULL) AS WI,
 MAX(t3.id IS NOT NULL) AS SI 
FROM students s
LEFT JOIN table2 t2
ON s.id = t1.id
LEFT JOIN table3 t3
ON s.id = t3.id
GROUP BY s.id

答案 3 :(得分:0)

假设WHERE条件正确-您可以在SELECT子句中使用相同的条件:

SELECT
    CARE1.students.first_name,
    CARE1.students.grade,
    CARE1.students.id, 
    CARE1.student_test_info.test_num,
    CARE1.student_test_info.date_of_testing,
    id IN (SELECT DISTINCT studentID from CARE1.WI) as WI, -- is always 1 due to WHERE condition
    id IN (SELECT DISTINCT studentID from CARE1.SI) as SI,
    id IN (SELECT DISTINCT studentID from CARE1.OC) as OC,
    0 as SL -- What is SL?
FROM CARE1.students
INNER JOIN CARE1.student_test_info
    ON CARE1.students.id = ARE1.student_test_info.student_id
WHERE id IN (SELECT DISTINCT studentID from CARE1.WI)
  AND (id NOT IN (SELECT DISTINCT studentID from CARE1.SI)
    OR id NOT IN (SELECT DISTINCT studentID from CARE1.OC))

但是-我建议使用EXISTS子查询而不是IN。并且还使用带有已计算列别名的HAVING子句来避免代码重复:

SELECT
    CARE1.students.first_name,
    CARE1.students.grade,
    CARE1.students.id, 
    CARE1.student_test_info.test_num,
    CARE1.student_test_info.date_of_testing,
    1 as WI, -- is always 1 due to WHERE condition
    EXISTS (SELECT * from CARE1.SI WHERE CARE1.SI.studentID = CARE1.students.id) as SI,
    EXISTS (SELECT * from CARE1.OC WHERE CARE1.OC.studentID = CARE1.students.id) as OC,
    0 as SL -- What is SL?
FROM CARE1.students
INNER JOIN CARE1.student_test_info
    ON CARE1.students.id = ARE1.student_test_info.student_id
WHERE EXISTS (SELECT * from CARE1.WI WHERE CARE1.WI.studentID = CARE1.students.id)
HAVING SI = 0
    OR OC = 0