到目前为止,我一直试图根据两列找到缺少的一对数字,我成功地获得了基于这个问题的解决方案,但是每当我为id添加另一列时,它就中断了。这是我所做的修改查询。
模式
CREATE TABLE `your_table` (
`id_session` int not null,
`columnstart` varchar(8) NOT NULL,
`columnend` varchar(8) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `your_table` (`id_session`,`columnstart`, `columnend`) VALUES
(1, 1, 1),
(1, 1, 3),
(1, 2, 1),
(1, 2, 2),
(1, 2, 3),
(2, 1, 1),
(2, 1, 2),
(2, 1, 3),
(2, 2, 1),
(2, 3, 1);
查询
SELECT dt.columnstart,
dt.columnend
FROM
(
SELECT t1.columnstart, t2.columnend FROM
(SELECT columnstart FROM your_table where id_session = 2 group by columnstart) AS t1
CROSS JOIN
(SELECT columnend FROM your_table where id_session = 2 group by columnend) AS t2
) AS dt
LEFT JOIN your_table AS t3
ON t3.columnstart = dt.columnstart AND
t3.columnend = dt.columnend
WHERE t3.columnstart IS NULL AND
t3.columnend IS NULL
查询结果是
3-2
3-3
缺少对是(2-2),(2-3),(3-2),(3-3)
(正确答案),请注意:1-3 != 3-1
发布于 2021-09-21 12:22:16
如果我正确理解它,您将搜索特定id_session
的缺失组合(如图2所示)。
在这种情况下,必须将AND t3.id_session = 2
添加到联接ON - http://sqlfiddle.com/#!9/d8fcb5/5中。
https://dba.stackexchange.com/questions/299910
复制相似问题