Mysql 忽略记录I';";跳过“;?

Mysql 忽略记录I';";跳过“;?,mysql,Mysql,我有两个表格:tableA和tableB tableA ------ id ... tableB ------ id tableA_id user_id 如果用户没有足够的信息来处理项目,他们会“跳过”它;这将向tableB添加一行,其中包含tableA中项目的id及其用户id 我想从tableA中获取用户未跳过但其他用户可能已跳过的行 例如: userA enters the queue userA is assigned item1 userA skips item1 userB en

我有两个表格:
tableA
tableB

tableA
------
id
...

tableB
------
id
tableA_id
user_id
如果用户没有足够的信息来处理项目,他们会“跳过”它;这将向
tableB
添加一行,其中包含
tableA
中项目的id及其用户id

我想从
tableA
中获取用户未跳过但其他用户可能已跳过的行

例如:

userA enters the queue
userA is assigned item1
userA skips item1

userB enters the queue
userB is assigned item1
userB skips item1

userA enters the queue
userA is assigned item2

userB enters the queue
userB is assigned item3

userC enters the queue
userC is assigned item1
到目前为止,我已经:

SELECT *
FROM tableA
LEFT OUTER JOIN tableB ON tableA.id = tableB.tableA_id
WHERE tableB.user_id IS NULL OR tableB.user_id != %s
GROUP BY tableA.id
;
由于
user\u id
不再为
NULL
而被任何用户跳过后,这将为所有其他用户返回
item1
。这可以防止其他用户跳过该项目

我如何完成我要做的事情?

试试这个:

select * from tableA
where tableA.id not in
(select tableB.tableA_id from tableB where tableB.user_id = %s)

您可以将查询更改为以下内容:

SELECT tableA.*
FROM tableA
LEFT OUTER JOIN (SELECT * FROM tableB WHERE user_Id = %s) tableB ON tableA.id = tableB.tableA_id
WHERE tableB.Id IS NULL

要从另一个表中没有匹配行的表中返回行,典型的模式是反联接:

SELECT a.id
  FROM `tableA` a
  LEFT 
  JOIN `tableB` b 
    ON b.tablea_id = a.id
   AND b.user_id   = %s
 WHERE b.user_id IS NULL
 ORDER
    BY a.id
我们将返回
a
中的所有行以及
b
中的所有匹配行。它是一个外部联接,因此将返回
a
中不匹配的行,以及
b
中包含所有空值的伪占位符行。诀窍是
WHERE
子句中的条件,它过滤掉所有匹配的行,只留下
a
中不匹配的行

我们可以使用不存在与相关子查询实现等效结果:

SELECT a.id
  FROM `tableA` a
 WHERE NOT EXISTS
       ( SELECT 1
           FROM `tableB` b 
          WHERE b.tablea_id = a.id
            AND b.user_id   = %s
       )
 ORDER
    BY a.id

这是有效的,但您可能想编辑您的答案,因为子查询无效。@特拉维斯,您是对的,我忘记了“from”子句,这是我的一个坏习惯。现在修好了。