0

我的两个 SQL 查询如下:-

1)

SELECT Pk_class_id 
FROM tbl_student 
JOIN tbl_class on tbl_student.fk_class_id = tbl_class.pk_class_id 
JOIN tbl_staff on tbl_staff.Pk_staff_id = tbl_class.fk_staff_id 
WHERE tbl_student.updated_at <= now() - INTERVAL 30 DAY

2)

SELECT Pk_class_id 
FROM tbl_student 
JOIN tbl_class on tbl_student.fk_class_id = tbl_class.pk_class_id 
JOIN tbl_staff on tbl_staff.Pk_staff_id = tbl_class.fk_staff_id 
WHERE tbl_staff.reg_time >= tbl_student.updated_at - INTERVAL 1 MINUTE

两个查询都运行没有错误,而我需要将其变成一个查询,例如(1+!2) 我需要与第二个查询相反的查询与第一个查询相关联。


我试图得到第二个查询的相反结果,如下所示: -

SELECT Pk_class_id 
FROM tbl_student 
JOIN tbl_class on tbl_student.fk_class_id = tbl_class.pk_class_id 
JOIN tbl_staff on tbl_staff.Pk_staff_id = tbl_class.fk_staff_id 
WHERE tbl_staff.reg_time 
**NOT IN**
(
SELECT Pk_class_id 
FROM tbl_student 
JOIN tbl_class on tbl_student.fk_class_id = tbl_class.pk_class_id 
JOIN tbl_staff on tbl_staff.Pk_staff_id = tbl_class.fk_staff_id 
WHERE  tbl_staff.reg_time >= tbl_student.updated_at -INTERVAL 1 MINUTE
)

结果:错误 1241 (21000):操作数应包含 1 列

有什么建议可以得到第二个查询的相反结果并与第一个查询结合吗?

任何一段代码表示赞赏并提前感谢。

4

3 回答 3

0
SELECT name, mobile, email 
  FROM tbl_student
  LEFT 
  JOIN tbl_class
    ON tbl_student.fk_class_id = tbl_class.pk_class_id
  LEFT 
  JOIN tbl_staff
ON tbl_staff.Pk_staff_id = tbl_class.fk_staff_id
 WHERE tbl_student.upd_time <= now() - INTERVAL 30 DAY AND fk_class_id NOT IN 
(SELECT fk_class_id 
  FROM tbl_student
  LEFT 
  JOIN tbl_class
    ON tbl_student.fk_class_id = tbl_class.pk_class_id
  LEFT 
  JOIN tbl_staff
ON tbl_staff.Pk_staff_id = tbl_class.fk_staff_id
 WHERE
 tbl_staff.registered_at >= tbl_student.upd_time - INTERVAL 1 MINUTE);
于 2014-07-15T12:39:00.630 回答
0
SELECT x.* 
  FROM my_table_instance x
  LEFT 
  JOIN my_other_table_instance y
    ON y.something = x.something
 WHERE y.something IS NULL
于 2014-07-15T10:58:50.430 回答
-1

您正在子查询中使用 pk_id 检查 tcl_staff.reg_time。或者您可以在这种类型中使用其简单而有用的左连接。

SELECT Pk_class_id 
FROM tbl_student 
JOIN tbl_class on tbl_student.fk_class_id = tbl_class.pk_class_id 
JOIN tbl_staff on tbl_staff.Pk_staff_id = tbl_class.fk_staff_id 
WHERE tbl_staff.Pk_class_id
**NOT IN**
(
SELECT Pk_class_id 
FROM tbl_student 
JOIN tbl_class on tbl_student.fk_class_id = tbl_class.pk_class_id 
JOIN tbl_staff on tbl_staff.Pk_staff_id = tbl_class.fk_staff_id 
WHERE  tbl_staff.reg_time >= tbl_student.updated_at -INTERVAL 1 MINUTE
)    
于 2014-07-15T11:00:54.363 回答