2014-10-02 3 views
-2

데이터베이스에 다음 테이블이 있습니다. 사용자가 구입 한 모든 책과 특정 책을 구입 한 모든 사용자를 결정해야합니다. SQL 쿼리가 올바르게 실행되지 않습니다.

http://i.imgur.com/IPb0bmi.png

내 SQL 요청하지만, 뭔가 잘못 :

select * 
from Livres 
where idCatalogue_fk in (
    select idCatalogue_fk 
    from ProduitsEtCommandes 
    where idCommande_fk in (
     select idCommande_fk 
     from CommandesEtUtilisateurs 
     where idUtilisateur_fk=1 
    ) 
); 


select * 
from Utilisateurs 
where idUtilisateur in (
    select idUtilisateur_fk 
    from CommandesEtUtilisateurs 
    where idCommande_fk in (
     select idCommande_fk 
     from Catalogue 
     where idCatalogue in (
      Select idLivre 
      from Livres 
      where idCatalogue=1 
      and title='Poezii' 
     ) 
    ) 
); 
+0

이러한 중첩'SELECT' 문이다. 'JOIN'과 함께하는 것이 더 낫다. – ne1410s

+1

뭐가 잘못 됐니? 샘플 데이터와 원하는 출력을 제공 할 수 있습니까? 이 세부 정보 중 일부를 포함하도록 질문을 수정하십시오. –

+0

"무언가가 잘못되었다"는 것은 무엇을 의미합니까? – Kritner

답변

2

나는 프랑스어 모르는 ... 테이블 및 열 이름과 철자 오류가있을 수 있습니다. .. where을 사용하여 특정 사용자 또는 도서에 대한 필터를 추가하십시오.

편집

그냥이 일을 할 수 있습니까?

: 데이터 모델에 따라 ... 기본적으로

select * from livres where idCatalogue_fk in ( 
    select idCatalogue_fk from produitsEtCommandes where idUtilisasteur_fk=1 
); 


-- note the misspelling comment i made up top... the ID in productsNCommands 
select * from utilisateurs where idUtilisateurs in ( 
     select idUtilisasteur_fk from produitsEtCommandes where idCatalogue_fk in (
           select idCatalogue_fk from livres where idLivre=1 
          ) 
    ); 

(이 숙제 인 경우,이 BTW ... 데이터베이스 모델의 아주 좋은 예가 아니다)을 수행해야

  • 카탈로그
  • 가 commandes 가입
  • produitsEtCommandes을 조인 리브르 가입

그리고 모든 사람들에게 지휘관 연합을 줄 것입니다. 필요에 따라 필터링하십시오.

는 사용자 김과 관련된 모든 책을 찾는 (해당 이름을 가진 한 명의 사용자 만 가정) : 그래서

select * 
from  livres 
     join catalogue on livres.idCatalogue_fk=catalogue.idCatalogue 
     join produitsEtCommandes on produitsEtCommandes.idCatalogue_fk=catalogue.idCatalogue 
     join commandes on commandes.idCommande=produitsEtCommandes.idCommande_fk 
where commandes.nomClient = 'Kim' 

.... 당신은이 작업을 수행 할 수 있습니다 .....?

select * from livres where idLivre in ( 
      select distinct idLivre 
      from  livres 
         join catalogue on livres.idCatalogue_fk=catalogue.idCatalogue 
         join produitsEtCommandes on produitsEtCommandes.idCatalogue_fk=catalogue.idCatalogue 
      where idCommandes_fk=1) 

또는 ... (그렇게 추한)

select * 
from  livres, catalogue, produitsEtCommandes, commandes 
where livres.idCatalogue_fk=catalogue.idCatalogue and 
     produitsEtCommandes.idCatalogue_fk=catalogue.idCatalogue and 
     commandes.idCommande=produitsEtCommandes.idCommande_fk and 
     livres.title = 'Brave New World' 

오, noes ...

select * from livres where idCatalogue_fk in (
     select distinct idCatalogue from catalogue where idCatalogue in (
         (select distinct idCatalogue_fk from produitsEtCommandes where idCommandes_fk=1) 
       ) 
    ) 
+0

문제는 내부 조인을 사용할 수 없다는 것입니다. 선택에서 선택하십시오 – Nicolae

+0

괜찮습니까 ... 정확히 그 이유는 무엇입니까? –

+0

@ gloomy.penguin 어쩌면 숙제 일 수도 있습니다 : P – Kritner

관련 문제