2013-08-23 30 views
1

在我的rails应用程序中,我有两个表 - device_portscircuits。我的目标是获得device_ports的列表,其id未被用于circuits表的physical_port_id列中。MySQL - 选择行的id不存在作为另一个表中的外键

我在其他表格上做过类似的事情,但这里我的查询只返回一行,当它返回23行时 - 这个设备有24个设备端口,一个正在使用。

select id, name, device_id, multiuse 
from device_ports 
where (device_id = 6 and multiuse = 1) 
or device_ports.id not in (select physical_port_id from circuits) 

所以这个查询获取所有多用途端口(这样即使ID是外键引用,该行仍然应该退还),并应该得到的所有行DEVICE_ID是6,但没有被引用电路,但只有多用途行正在返回。

从查询结果是

id | name | device_id | multiuse 
------------------------------------ 
268 | test-1 |  6  | 1 

我曾尝试创建一个SQL小提琴,但在构建似乎只是超时。

CREATE TABLE `device_ports` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `device_id` int(11) DEFAULT NULL, 
    `name` tinytext, 
    `speed` tinytext, 
    `multiuse` tinyint(1) DEFAULT NULL, 
    `created_at` datetime DEFAULT NULL, 
    `updated_at` datetime DEFAULT NULL, 
    PRIMARY KEY (`id`), 
    KEY `id` (`id`) 
) ENGINE=MyISAM AUTO_INCREMENT=291 DEFAULT CHARSET=latin1; 

INSERT INTO `device_ports` (`id`, `device_id`, `name`, `speed`, `multiuse`, `created_at`, `updated_at`) 
*emphasized text*VALUES 
(1, 1, 'Test Device Port', '100', 0, NULL, NULL), 
(2, 1, 'Test Port 2', '300', 1, NULL, NULL), 
(289, 6, 'test-22', '100', 0, NULL, NULL), 
(290, 6, 'test-23', '100', 0, NULL, NULL), 
(288, 6, 'test-21', '100', 0, NULL, NULL), 
(287, 6, 'test-20', '100', 0, NULL, NULL), 
(286, 6, 'test-19', '100', 0, NULL, NULL), 
(284, 6, 'test-17', '100', 0, NULL, NULL), 
(285, 6, 'test-18', '100', 0, NULL, NULL), 
(283, 6, 'test-16', '100', 0, NULL, NULL), 
(282, 6, 'test-15', '100', 0, NULL, NULL), 
(281, 6, 'test-14', '100', 0, NULL, NULL), 
(280, 6, 'test-13', '100', 0, NULL, NULL), 
(279, 6, 'test-12', '100', 0, NULL, NULL), 
(278, 6, 'test-11', '100', 0, NULL, NULL), 
(277, 6, 'test-10', '100', 0, NULL, NULL), 
(276, 6, 'test-9', '100', 0, NULL, NULL), 
(275, 6, 'test-8', '100', 0, NULL, NULL), 
(274, 6, 'test-7', '100', 0, NULL, NULL), 
(273, 6, 'test-6', '100', 0, NULL, NULL), 
(272, 6, 'test-5', '100', 0, NULL, NULL), 
(271, 6, 'test-4', '100', 0, NULL, NULL), 
(270, 6, 'test-3', '100', 0, NULL, NULL), 
(269, 6, 'test-2', '100', 0, NULL, NULL), 
(268, 6, 'test-1', '100', 1, NULL, NULL), 
(267, 6, 'test-0', '100', 0, NULL, NULL); 


CREATE TABLE `circuits` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `organisation_id` int(11) DEFAULT NULL, 
    `physical_port_id` int(11) DEFAULT NULL, 
    PRIMARY KEY (`id`) 
) ENGINE=InnoDB AUTO_INCREMENT=248 DEFAULT CHARSET=latin1; 

INSERT INTO `circuits` (`id`, `organisation_id`, `physical_port_id`) 
VALUES (1, 125, 267); 
+0

我想在我结束它上面贴的代码是给我25个记录.. –

+0

你的第一个表是MyISAM和第二个表InnoDB的..我希望没有发挥作用.. –

+0

@Sonali以及斑点......不确定这是否会产生影响,25个声音是否正确,因为这会给你23个来自设备6的结果和两个来自设备1的结果?我忘了将'AND device_id = 6'添加到查询中以将其过滤掉。 – martincarlin87

回答

7

你可以尝试使用LEFT OUTER JOIN:

SELECT DISTINCT d.id, d.name, d.device_id, d.multiuse 
FROM device_ports d 
LEFT OUTER JOIN circuits c ON c.physical_port_id = d.id 
WHERE 
(c.physical_port_id IS NULL AND d.device_id = 6) 
OR (d.multiuse = 1 AND d.device_id = 6) 
ORDER BY d.id 

这有几种查询技术,看看What's the difference between NOT EXISTS vs. NOT IN vs. LEFT JOIN WHERE IS NULL?

+0

我接近你的原始答案,但编辑只是给了我一行。这是我最近的尝试:'SELECT d.id,d.name,d.device_id,d.multiuse FROM device_ports d LEFT OUTER JOIN circuits c ON c.physical_port_id = d.id WHERE(c.physical_port_id IS NULL AND ORDER BY ID ASC'几乎是完美的,唯一的'错误'是,如果一个多用途端口IS引用它会出现两次结果。可能只是在ID上分组,但希望查询更聪明 – martincarlin87

+0

好的。我编辑了我的答案以使用WHERE子句。我添加了DISTINCT,是否解决了你重复的行? –

+0

完美,谢谢 – martincarlin87

0
SELECT p.* 
    FROM device_ports p 
    LEFT 
    JOIN circuits c 
    ON c.physical_port_id = p.id 
WHERE p.device_id = 6 
    AND multiuse = 1 
    AND c.id IS NULL; 
相关问题