多表sql中的Where子句加入

时间:2016-04-13 17:31:44

标签: sql

TABLES

CREATE TABLE LocalBusiness
(
    BusinessID INT NOT NULL PRIMARY KEY,
    BusinessName VARCHAR2 (20) NOT NULL,
    TypeID INT,
    Latitude DECIMAL (10,2),
    Longitude DECIMAL (10,2),
    Web_address VARCHAR2 (50) NOT NULL,
    Postcode VARCHAR2 (10) NOT NULL,
    official_rating int,
    min_price NUMBER(4,2),
    max_price NUMBER(4,2),

    FOREIGN KEY (TypeID) REFERENCES LocalBusinessType (TypeID),
    CONSTRAINT chk_Officialrating CHECK (official_rating> 0 AND  official_rating<6 )
);

CREATE TABLE Address
(
    AddressID INT NOT NULL PRIMARY KEY,
    BusinessID INT,
    AreaID INT,
    Address VARCHAR2 (50) NOT NULL,
    Postcode VARCHAR2 (10) NOT NULL,

    FOREIGN KEY (BusinessID) REFERENCES LocalBusiness (BusinessID),
    FOREIGN KEY (AreaID) REFERENCES Area (AreaID)
);

CREATE TABLE Phone
(
    PhoneNoID INT NOT NULL PRIMARY KEY,
    PhoneNo VARCHAR2 (15) NOT NULL, 
    BusinessID INT,
    Description VARCHAR2 (50) NOT NULL,

    FOREIGN KEY (BusinessID) REFERENCES LocalBusiness (BusinessID),
    CONSTRAINT PhoneNo_unique UNIQUE (PhoneNo) 
);

CREATE TABLE Email
(
    EmailID INT NOT NULL PRIMARY KEY,
    email_address VARCHAR2 (50) NOT NULL,
    BusinessID INT,
    Description VARCHAR2 (50) NOT NULL,

    FOREIGN KEY (BusinessID) REFERENCES LocalBusiness (BusinessID),
    CONSTRAINT email_unique UNIQUE (email_address)
);

CREATE TABLE Area
(
    AreaID INT NOT NULL PRIMARY KEY,
    AreaName VARCHAR2 (20) NOT NULL,
    Region VARCHAR2 (20) NOT NULL
);

SELECT声明:

SELECT  
    LocalBussiness.BusinessName, Address.Address, Address.Postcode, 
    Area.AreaName, Area.Region, LocalBusiness.OfficialRating, 
    LocalBusiness.min_price, LocalBusiness_max_price, 
    Phone.description, Phone.PhoneNo, 
    Email.Description, Email.email_address, 
    LocalBusiness.Web_address
FROM
    LocalBusiness 
JOIN 
    Address ON LocalBusiness.BusinessID = Address.BusinessID
JOIN 
    Area ON Address.AreaID = Area.AreaID 
         AND LocalBusiness.BusinessID = Address.BusinessID
JOIN 
    Phone ON Phone.BusinessID = LocalBusiness.BusinessID
JOIN 
    Email ON Email.BusinessID = LocalBusiness.BusinessID
WHERE 
    TypeID = '1'
ORDER BY 
    LocalBusiness.BusinessName ASC;

上面写的sql join语句中的where子句似乎无效,因为TypeID的某些值返回不完整的数据,而其他值根本没有返回任何行。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

你重复一个条件

AND LocalBusiness.BusinessID=Address.BusinessID in JOIN Area

尝试避免重复

SELECT LocalBussiness.BusinessName, Address.Address, Address.Postcode,  
        Area.AreaName, Area.Region, LocalBusiness.OfficialRating, 
        LocalBusiness.min_price, LocalBusiness_max_price, Phone.description,
        Phone.PhoneNo, Email.Description, Email.email_address, 
        LocalBusiness.Web_address
FROM LocalBusiness 
JOIN Address ON LocalBusiness.BusinessID=Address.BusinessID
JOIN Area ON Address.AreaID=Area.AreaID 
JOIN Phone ON Phone.BusinessID=LocalBusiness.BusinessID
JOIN Email ON Email.BusinessID=LocalBusiness.BusinessID
WHERE TypeID = '1'
ORDER BY LocalBusiness.BusinessName ASC;

否则检查数据是否一致..