MySQL查询WHERE条件

时间:2015-01-26 15:31:58

标签: mysql

我在MySQL数据库中有 doTable 表。 在 doTable 中,我有三个字段:

IdCode  int 11
State   char 2
id      int 11

单个 State 的字段 IdCode 的值可以是:A,B,C,D或TC。

+---------+-------+----+
| IdCode  | State | id |
+---------+-------+----+
| 6027713 | TC    |  1 |
| 7057816 | B     |  2 |
| 7047412 | TC    |  3 |
| 7047412 | A     |  4 |
| 6027713 | B     |  5 |
| 6027713 | B     |  6 |
| 6027713 | C     |  7 |
| 6040309 | TC    |  8 |
| 6040309 | A     |  9 |
| 6040309 | TC    | 10 |
| 6040309 | A     | 11 |
| 4041208 | TC    | 12 |
| 7060912 | TC    | 13 |
| 7060912 | D     | 14 |
+---------+-------+----+

我需要提取 doTable 的所有行,其中 State 值仅在相同 IdCode时为TC 包含A,B,C或D.

我需要这个输出:

+---------+-------+----+
| IdCode  | State | id |
+---------+-------+----+
| 6027713 | TC    |  1 |
| 7057816 | B     |  2 |
| 7047412 | TC    |  3 |
| 7047412 | A     |  4 |
| 6027713 | B     |  5 |
| 6027713 | B     |  6 |
| 6027713 | C     |  7 |
| 6040309 | TC    |  8 |
| 6040309 | A     |  9 |
| 6040309 | TC    | 10 |
| 6040309 | A     | 11 |
| 7060912 | TC    | 13 |
| 7060912 | D     | 14 |
+---------+-------+----+

并在输出中排除ID为12的行,因为我没有相同IdCode的值A,B,C或D

+---------+-------+----+
| IdCode  | State | id |
+---------+-------+----+
| 4041208 | TC    | 12 |
+---------+-------+----+

我尝试了这个查询但是空集。

mysql> SELECT
    *
FROM
    `dotable`
WHERE
    (
        State = 'A'
        OR State = 'B'
        OR State = 'C'
        OR State = 'D'
    )
AND State = 'TC';
Empty set

mysql> 

你能帮助我吗?

-- ----------------------------
-- Table structure for `dotable`
-- ----------------------------
DROP TABLE IF EXISTS `dotable`;
CREATE TABLE `dotable` (
  `IdCode` int(11) DEFAULT NULL,
  `State` char(2) DEFAULT NULL,
  `id` int(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=15 DEFAULT CHARSET=latin1;

-- ----------------------------
-- Records of dotable
-- ----------------------------
INSERT INTO `dotable` VALUES ('6027713', 'TC', '1');
INSERT INTO `dotable` VALUES ('7057816', 'B', '2');
INSERT INTO `dotable` VALUES ('7047412', 'TC', '3');
INSERT INTO `dotable` VALUES ('7047412', 'A', '4');
INSERT INTO `dotable` VALUES ('6027713', 'B', '5');
INSERT INTO `dotable` VALUES ('6027713', 'B', '6');
INSERT INTO `dotable` VALUES ('6027713', 'C', '7');
INSERT INTO `dotable` VALUES ('6040309', 'TC', '8');
INSERT INTO `dotable` VALUES ('6040309', 'A', '9');
INSERT INTO `dotable` VALUES ('6040309', 'TC', '10');
INSERT INTO `dotable` VALUES ('6040309', 'A', '11');
INSERT INTO `dotable` VALUES ('4041208', 'TC', '12');
INSERT INTO `dotable` VALUES ('7060912', 'TC', '13');
INSERT INTO `dotable` VALUES ('7060912', 'D', '14');

3 个答案:

答案 0 :(得分:1)

试试这个:

SELECT *
FROM `dotable`
WHERE IdCode IN 
    (SELECT IdCode 
     FROM `dotable`
     WHERE State IN ('A','B','C','D'))

<强>解释

内部查询从值A,B,C和D中选择至少为一个状态的IdCodes。外部查询从内部查询的结果中选择具有IdCodes的所有记录。

<强>结果:

IDCODE  STATE   ID
6027713 TC      1
7057816 B       2
7047412 TC      3
7047412 A       4
6027713 B       5
6027713 B       6
6027713 C       7
6040309 TC      8
6040309 A       9
6040309 TC      10
6040309 A       11
7060912 TC      13
7060912 D       14

请参阅SQL Fiddle中的结果。

答案 1 :(得分:0)

尝试:

select f.*
from dotable f
join (select distinct idcode
      from dotable 
      where state in ('A','B','C','D') ) x
on x.idcode = f.idcode

SQLFiddle Here.

答案 2 :(得分:0)

以下代码填补了您的需求

mysql> SELECT
    *
FROM
    `dotable`
WHERE
    (
        State = 'A'
        OR State = 'B'
        OR State = 'C'
        OR State = 'D'
        AND IdCode = (SELECT IdCode WHERE State = 'TC')
    )
;