SELECT D.Name,D.Id
FROM category_devices AS CD JOIN
Devices AS D
ON CD.deviceId = D.Id
Where CD.DeviceId = D.Id AND D.AppId =2
这是我的查询,我想显示CD.DeviceId!= D.Id的所有设备。 上面的查询显示了所有相等的设备,如何将该查询转换为不等于?
SELECT D.Name,D.Id
FROM category_devices AS CD JOIN
Devices AS D
ON CD.deviceId = D.Id
Where CD.DeviceId = D.Id AND D.AppId =2
答案 0 :(得分:0)
您可以使用LEFT JOIN
和WHERE
:
SELECT D.Name, D.Id
FROM category_devices CD LEFT JOIN
Devices D
ON CD.deviceId = D.Id AND D.AppId = 2
WHERE D.Id IS NULL;
但是,由于没有匹配项,因此只会在NULL
中返回SELECT
值。想必您想要这样的东西:
SELECT CD.*
FROM category_devices CD LEFT JOIN
Devices D
ON CD.deviceId = D.Id AND D.AppId = 2
WHERE D.Id IS NULL;
答案 1 :(得分:0)
尚不清楚您想要什么,但是如果您想知道category_devices中的信息,而不是设备中的信息,以及设备中但category_devices信息中的信息,则不知道-
drop table if exists category_devices,devices;
create table category_devices(deviceId int);
create table devices(id int,name varchar(2));
insert into category_devices values (1),(4);
insert into devices values (1,'aa'),(2,'bb'),(3,'cc');
select cd.deviceid,d.id,d.name
from category_devices cd
left join devices d on cd.deviceid = d.id
where d.id is null
union
select cd.deviceid,d.id,d.name
from devices d
left join category_devices cd on cd.deviceid = d.id
where cd.deviceid is null;
+----------+------+------+
| deviceid | id | name |
+----------+------+------+
| 4 | NULL | NULL |
| NULL | 2 | bb |
| NULL | 3 | cc |
+----------+------+------+
3 rows in set (0.00 sec)
答案 2 :(得分:0)
使用NOT LIKE运算符
SELECT D.Name,D.Id
FROM category_devices AS CD JOIN
Devices AS D
ON CD.deviceId = D.Id
Where CD.DeviceId NOT LIKE D.Id AND D.AppId =2
更多信息,请点击SQL docs