有一个表table
:
CREATE TABLE IF NOT EXISTS `table` (
`main` int(11) NOT NULL,
`sid` int(11) DEFAULT NULL,
`rid` int(11) DEFAULT NULL,
`nid` text
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `table`(
`main`, `sid`, `rid`, `nid`
) VALUES(
1, 8, 1, null),
(1, null, null, "hello"),
(2, 8, 1, null),
(2, 1, 2, null),
(1, 1, 2, null)
我需要找到字段main
,其中记录包含下一个数据:
sid = 8, rid=1, nid = null ;
sid = null, rid = null, nid = "hello"
sid=1, rid=2, nid = null
例如步骤:
答案 0 :(得分:0)
试试这个: -
SELECT main FROM `table` where sid IS NULL AND rid IS NULL AND nid = 'hello' AND
main IN (SELECT main FROM `table` WHERE sid = 8 AND rid=1 AND nid IS NULL);
希望这会对你有所帮助。