我正在尝试解决这个SQL查询(用法语)
我有4个不同的表:CLIENT,COMMANDE(订单),DETAIL(订单)和PRODUIT(产品)
我必须在查询中提供尚未发出任何订单(COMMANDE)的'Namur'(即LOCALITE)客户的号码(NCLI)和名称(NOM)
我尝试了不同的东西,我在SQL快递中的最后一个代码是这个(当然不起作用。
select NCLI, NOM
from CLIENT
where LOCALITE = 'Namur' and (NCLI in (select NCLI
from COMMANDE
where NCOM in (select NCOM
from DETAIL
where QCOM not in DETAIL)))
有人可以帮我解决这个问题吗?
如果需要,这是我现有表格和插入内容的代码:
create table CLIENT
(NCLI char(4) not null,
NOM varchar(12) not null,
ADRESSE varchar(20) not null,
LOCALITE varchar(12) not null,
CAT char(2),
COMPTE decimal(9,2) not null,
primary key (NCLI));
create table PRODUIT
(NPRO char(5) not null,
LIBELLE varchar(20) not null,
PRIX decimal(5,0) not null,
QSTOCK decimal(6,0) not null,
primary key (NPRO));
create table COMMANDE
(NCOM char(5) not null,
NCLI char(4) not null,
DATECOM datetime not null,
primary key (NCOM),
foreign key (NCLI) references CLIENT);
create table DETAIL
(NCOM char(5) not null,
NPRO char(5) not null,
QCOM decimal(4,0) not null,
primary key (NCOM,NPRO),
foreign key (NCOM) references COMMANDE,
foreign key (NPRO) references PRODUIT);
insert into CLIENT values ('B112','HANSENNE' ,'23, a. Dumont' ,'Poitiers' ,'C1',1250.00);
insert into CLIENT values ('C123','MERCIER' ,'25, r. Lemaitre' ,'Namur' ,'C1',-2300);
insert into CLIENT values ('B332','MONTI' ,'112, r. Neuve' ,'Geneve' ,'B2',0);
insert into CLIENT values ('F010','TOUSSAINT' ,'5, r. Godefroid' ,'Poitiers' ,'C1',0);
insert into CLIENT values ('K111','VANBIST' ,'180, r. Florimont' ,'Lille' ,'B1',720);
insert into CLIENT values ('S127','VANDERKA' ,'3, av. des Roses' ,'Namur' ,'C1',-4580);
insert into CLIENT values ('B512','GILLET' ,'14, r. de l''Ete' ,'Toulouse' ,'B1',-8700);
insert into CLIENT values ('B062','GOFFIN' ,'72, r. de la Gare' ,'Namur' ,'B2',-3200);
insert into CLIENT values ('C400','FERARD' ,'65, r. du Tertre' ,'Poitiers' ,'B2',350);
insert into CLIENT values ('C003','AVRON' ,'8, ch. de la Cure' ,'Toulouse' ,'B1',-1700);
insert into CLIENT values ('K729','NEUMAN' ,'40, r. Bransart' ,'Toulouse' ,NULL,0);
insert into CLIENT values ('F011','PONCELET' ,'17, Clos des Erables','Toulouse' ,'B2',0);
insert into CLIENT values ('L422','FRANCK' ,'60, r. de Wepion' ,'Namur' ,'C1',0);
insert into CLIENT values ('S712','GUILLAUME' ,'14a, ch. des Roses' ,'Paris' ,'B1',0);
insert into CLIENT values ('D063','MERCIER' ,'201, bvd du Nord' ,'Toulouse' ,NULL,-2250);
insert into CLIENT values ('F400','JACOB' ,'78, ch. du Moulin' ,'Bruxelles','C2',0);
insert into PRODUIT values ('CS262','CHEV. SAPIN 200x6x2', 75, 45);
insert into PRODUIT values ('CS264','CHEV. SAPIN 200x6x4', 120,2690);
insert into PRODUIT values ('CS464','CHEV. SAPIN 400x6x4', 220, 450);
insert into PRODUIT values ('PA45' ,'POINTE ACIER 45 (1K)',105, 580);
insert into PRODUIT values ('PA60' ,'POINTE ACIER 60 (1K)', 95, 134);
insert into PRODUIT values ('PH222','PL. HETRE 200x20x2', 230, 782);
insert into PRODUIT values ('PS222','PL. SAPIN 200x20x2', 185,1220);
insert into COMMANDE values ('30178','K111','20081221');
insert into COMMANDE values ('30179','C400','20081222');
insert into COMMANDE values ('30182','S127','20081223');
insert into COMMANDE values ('30184','C400','20081223');
insert into COMMANDE values ('30185','F011','20090102');
insert into COMMANDE values ('30186','C400','20090102');
insert into COMMANDE values ('30188','B512','20090103');
insert into DETAIL values ('30178','CS464',25);
insert into DETAIL values ('30179','PA60',20);
insert into DETAIL values ('30179','CS262',60);
insert into DETAIL values ('30182','PA60',30);
insert into DETAIL values ('30184','CS464',120);
insert into DETAIL values ('30184','PA45',20);
insert into DETAIL values ('30185','PA60',15);
insert into DETAIL values ('30185','PS222',600);
insert into DETAIL values ('30185','CS464',260);
insert into DETAIL values ('30186','PA45',3);
insert into DETAIL values ('30188','PA60',70);
insert into DETAIL values ('30188','PH222',92);
insert into DETAIL values ('30188','CS464',180);
insert into DETAIL values ('30188','PA45',22);
答案 0 :(得分:1)
为什么不试试这个:
select NCLI,NOM
from CLIENT
WHERE LOCALITE = 'NAMUR' AND NOT EXISTS(
SELECT *
FROM COMMANDE
WHERE CLIENT.NCLI = COMMANDE .NCLI)
答案 1 :(得分:0)
这有用吗?
select c.NCLI, c.NOM
from CLIENT c
where c.LOCALITE = 'Namur' and c.NCLI NOT IN (select NCLI
from COMMANDE)