dimanche 15 mars 2015

selection in a many to many relationship

hey there , i am working on my highschool project i want to run a query for user to search medicines using multiple categories here is the ddl,



Create table medicine (
MedId int(11) NOT NULL AUTO_INCREMENT,
Name varchar(30) COLLATE utf8_bin DEFAULT NULL,
Vendor varchar(20) COLLATE utf8_bin DEFAULT NULL,
Quantity int(11) NOT NULL,
type text COLLATE utf8_bin NOT NULL,
price int(11) NOT NULL,
PRIMARY KEY (MedId)
);

create table category(
CatId smallint auto_increment,
Name varchar (20),
primary key(CatID)
);

create table MedicineCat(
MedId int,
CatId smallint,
foreign key (MedId) references medicine(MedId),
foreign key (CatId) references category(CatId),
primary key(MedId,CatId)


);


SELECT medicinecat.MedID AS 'MedId', medicine.Name AS 'Name', medicine.price AS 'Price', medicine.Quantity AS 'Quantity'
FROM medicinecat
JOIN medicine ON medicine.MedId = medicinecat.MedId
WHERE CatId in (1,2,3);


what i am getting is all the medicines that has at least one of the category , i know thats because of in clause, what i want is to select medicine that has "all the categories" not one of the category .


thanks in advance


Aucun commentaire:

Enregistrer un commentaire