I have the table doTable in MySQL database.
In doTable I have three fields:
IdCode int 11
State char 2
id int 11
The values of field State for single IdCode could be: A, B, C, D or 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 |
+---------+-------+----+
I need extract all rows of doTable where the State value are TC only when the same IdCode contains A, B, C or D.
I need this output:
+---------+-------+----+
| 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 |
+---------+-------+----+
And exclude in the output the row with ID number 12 because I don't have values A, B, C or D for the same IdCode
+---------+-------+----+
| IdCode | State | id |
+---------+-------+----+
| 4041208 | TC | 12 |
+---------+-------+----+
I have tried this query but Empty set.
mysql> SELECT
*
FROM
`dotable`
WHERE
(
State = 'A'
OR State = 'B'
OR State = 'C'
OR State = 'D'
)
AND State = 'TC';
Empty set
mysql>
Can you help me?
-- ----------------------------
-- 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');
AND State='TC'in the where clause?State = 'TC'