MySQL Forums
Forum List  »  Newbie

Using SELECT within SELECT in mysql query
Posted by: Daniel Le Breton
Date: March 21, 2013 03:01PM

Hello,

I want to obtain:

regroupement_id item

2 31

but I obtain 2 rows using this request :

2 31

100 3

SELECT DISTINCT ra.regroupement_id, ra.item
FROM regroupement_item AS ra
INNER JOIN regroupement ON ra.regroupement_id = regroupement.regroupement_id
WHERE regroupement.regroupement_type =2
AND ra.item NOT
IN (
SELECT ra1.item
FROM regroupement_item AS ra1
INNER JOIN regroupement ON ra1.regroupement_id = regroupement.regroupement_id
WHERE regroupement.regroupement_type =1

)

What must I add to obtain one row,

I don't want row : item 3 regroupement_id 100

record which not has regroupement type 1

(valid record is item 31 regoupement_id 2 which has

1 record type 1 using item 5 et 8)

In advance, many thanks for your help.



CREATE TABLE IF NOT EXISTS `regroupement_item` (
`item` int(11) NOT NULL,
`regroupement_id` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `regroupement_item` (`item`, `regroupement_id`) VALUES
(5, 1),
(8, 1),
(10, 1),
(17, 1),
(5, 2),
(8, 2),
(31, 2),
(10, 3),
(17, 3),
(3, 100);

CREATE TABLE IF NOT EXISTS `regroupement` (
`regroupement_id` int(11) NOT NULL AUTO_INCREMENT,
`regroupement_type` int(11) NOT NULL,
`nom` varchar(25) NOT NULL,
PRIMARY KEY (`regroupement_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=101 ;

INSERT INTO `regroupement` (`regroupement_id`, `regroupement_type`, `nom`) VALUES
(1, 1, 'principal_1'),
(2, 2, 'secondaire_2'),
(3, 2, 'secondaire_3'),
(4, 1, 'principal_2'),
(100, 2, 'secondaire_100');

Options: ReplyQuote




Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.