2012-08-15 3 views
-5

가능한 중복 : 나는 하나를 참조 answered..didn't 이미 뭔가를 요구하고 있지 않다 희망
MySQL SELECT results from 1 table, but exclude results depending on another table?SELECT *에서 한 테이블의 결과를 다른 테이블에서 제외 하시겠습니까?

. 간단하지만 나는 그것을 작동시킬 수 없습니다.

두 개의 테이블이 있습니다 : 단위와 임대.

임대 테이블의 종료일 필드에 null이있는 유닛을 제외한 유닛 테이블의 모든 열린 유닛 (건물 및 유닛 유닛 번호)의 목록을 원합니다. 그러나 당신이 "하지에서"사용하는 것이, 다른 데이터베이스 엔진에서

select u.* 
from units u 
where not exists (select 1 from lease l where l.unitkey = u.unitkey and l.enddate is null) 

:.

CREATE TABLE Unit(
    UnitKey  Int NOT NULL AUTO_INCREMENT, 
    UnitNumber Char(5) NOT NULL, 
    BuildingKey Int  NOT NULL, 
    CONSTRAINT UNIT_PK PRIMARY KEY(UnitKey), 
    CONSTRAINT UNIT_BLDG_FK  FOREIGN KEY(BuildingKey) 
        REFERENCES Building(BuildingKey)); 

CREATE TABLE Lease(
    LeaseKey Int     NOT NULL AUTO_INCREMENT, 
    UnitKey Int     NOT NULL, 
    EndDate  Date     NULL, 
    CONSTRAINT LEASE_PK    PRIMARY KEY(LeaseKey), 
    CONSTRAINT  LEASE_UNIT_FK FOREIGN KEY(UnitKey) REFERENCES Unit(UnitKey)); 
+2

유사 항목 : http://stackoverflow.com/questions/4186242/sql-select-all-unique-values-in-table-a-which-are-not-in-table-a-which-are-not-in-table- b 및 http://stackoverflow.com/questions/4660871/mysql-se table-a-if-if-exist-in-table-b – Charx

+2

귀하의 질문에 여러 번 답변되었습니다 : http://stackoverflow.com/questions/2862778/mysql-select-results - 다른 테이블에 따라 - 테이블 1에서 제외되는 결과 - 결과 1 = 다른? rq = 1 http://stackoverflow.com/questions/10255266/select-users-from-one-table-only-if-not-in-other-rother=rq=1 – Jocelyn

답변

1

이 시도 : 테이블에 대한

구조 (내가이 예를 들어, 필요하지 다른 필드를 제거 이것은 mysql에서 더 잘 최적화된다.

관련 문제