0

http://sqlfiddle.com/#!9/df6245

How to delete all rows Where brand not equal to 107 for example in above fiddle GRPCD 44111 have 107 so it should not delete both rows which have GRPCD 44111 but it will delete both two rows of GRPCD 33111 because there is no brand 107 value. Please help Edit This query is working perfect someone can give me better query.

DELETE FROM `products_cross` WHERE GRPCD NOT IN (SELECT GRPCD FROM products_cross WHERE brand=107) 
0

1 Answer 1

0

You need a subquery like this for the products_cross , so that Mysql has a temporary table to select the wanted ids

CREATE TABLE IF NOT EXISTS `products_cross` (
  `id` int(6) unsigned NOT NULL,
  `GRPCD` int(10) unsigned NOT NULL,
  `brand` int(5) NOT NULL,
  PRIMARY KEY (`id`)
) DEFAULT CHARSET=utf8;
INSERT INTO `products_cross` (`id`, `GRPCD`, `brand`) VALUES
  ('1', '33111', '107'),
  ('2', '33111', '103'),
  ('3', '44111', '105'),
  ('4', '44111', '101');
SELECT * FROM products_cross
id | GRPCD | brand
-: | ----: | ----:
 1 | 33111 |   107
 2 | 33111 |   103
 3 | 44111 |   105
 4 | 44111 |   101
DELETE FROM `products_cross` 
WHERE GRPCD NOT IN (SELECT GRPCD FROM (SELECT * FROM products_cross ) pc WHERE brand=107) 
SELECT * FROM products_cross
id | GRPCD | brand
-: | ----: | ----:
 1 | 33111 |   107
 2 | 33111 |   103

db<>fiddle here

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.