Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL查询,根据记录在第二个表上的状态丢弃记录_Mysql_Sql - Fatal编程技术网

MySQL查询,根据记录在第二个表上的状态丢弃记录

MySQL查询,根据记录在第二个表上的状态丢弃记录,mysql,sql,Mysql,Sql,我有两张桌子: CREATE TABLE qr_code ( id int(10) primary key auto_increment, code int(10) not null ); CREATE TABLE area_qrcode ( id int(10) primary key auto_increment, area_id int(10) not null, qr_code_id int(10) not null,

我有两张桌子:

CREATE TABLE qr_code (
    id   int(10) primary key auto_increment,
    code int(10) not null
);

CREATE TABLE area_qrcode (
    id         int(10) primary key auto_increment,
    area_id    int(10) not null,
    qr_code_id int(10) not null,
    status     set('open', 'close')
);
在我的表格中,我有以下记录:

-- qr_code
+----+------+
| id | code |
+----+------+
|  1 |  200 |
|  2 |  201 |
|  3 |  202 |
+----+------+

-- areaqr_code
+----+---------+------------+--------+
| id | area_id | qr_code_id | status |
+----+---------+------------+--------+
|  1 |       2 |          1 | open   |
|  2 |       4 |          3 | close  |
+----+---------+------------+--------+
我想从表
qr\u-code
中选择
区域qr-code
表中未处于打开状态的所有记录<代码>二维码.id对应于
区域二维码.qr码.id

一个好的结果集是:

+----+------+
| id | code |
+----+------+
|  2 |  201 |
|  3 |  202 |
+----+------+
我正在使用以下查询,但它并没有像我预期的那样工作,因为如果表
area\u qrcode
没有与
qr\u code
中的值对应的值,那么我就不会得到这些记录的结果:

SELECT
    qr.*
FROM
    `qr_code` as qr,
    `area_qrcode` as aq 
WHERE
    (
        aq.qr_code_id = qr.id and
        aq.status != 'open'
    )
    OR
    (
        aq.qr_code_id != qr.id
    );

即使在
区域qr\u code
中没有相关记录,我可以更改什么以从
qr\u code
获取记录?

您可以通过子选择获得结果:

SELECT 
    * 
FROM 
    `qr_code` as qr
where 
    qr.id NOT IN (
        SELECT qr_code_id 
        FROM area_qrcode 
        WHERE status = 'open'
    );

通过左连接,您也可以获得结果:

SELECT 
    qr.* 
FROM 
    qr_code as qr 
LEFT JOIN 
    area_qrcode as aq 
ON
    qr.id = aq.qr_code_id
AND
    aq.status = 'open'
WHERE
    aq.qr_code_id IS NULL;

第三种可能性(不像第一种那么直观):


选择qr.*从
qr\u code
作为qr左侧加入
区域qrcode
作为aq上的aq。qr\u code\u id=qr.id和aq.status!='open'或(aq.qr\u code\u id!=qr.id)

使用正确的连接,以防止混淆

SELECT qr_code.id,qr_code.code 
FROM qr_code 
LEFT JOIN area_qrcode 
ON area_qrcode.qr_code_id = qr_code.id
WHERE area_qrcode.status <> 'open' OR area_qrcode.status IS NULL
选择qr\u code.id、qr\u code.code
来自qr_码
左连接区域编码
在区域上\qrcode.qr\u code\u id=qr\u code.id
其中area_qrcode.status“open”或area_qrcode.status为空

如果我正确理解了您的问题,这就是您要查找的查询:

选择q.id,q.code
来自qr_码q
左连接区域\ QRU代码aq
在aq.qr\U代码上\U id=q.id
其中(aq.status为空或aq.status为“打开”)
将导致:

ID  CODE
--  ----
2   201
3   202

可用小提琴。

轻微的打字错误,请删除逗号
ID  CODE
--  ----
2   201
3   202