MySQL选择三个字段(如过滤器)

时间:2014-05-27 11:12:25

标签: php mysql select filter where

有一个表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

例如步骤:

  1. 首先,我们得到数据:sid = 8,rid = 1,nid = null
  2. 使用此数据,我们进行select-query,结果是两条记录:main = 1和main = 2,
  3. 首先,我们添加一个条件:sid = null,rid = null,nid =" hello"
  4. 现在查询必须过滤并返回main = 1
  5. 的一条记录

1 个答案:

答案 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);

希望这会对你有所帮助。