尝试加入多个表

时间:2015-12-09 10:44:20

标签: mysql database join view

Tables And what i need 我正在尝试为学校项目创建一个包含5个表的视图。到目前为止的陈述是这样的:

CREATE view lager AS 
Select produkt.produktNumber,
(SELECT buyphone.lagerNummer FROM bluecity.buyphone) AS 'Lagernummer',
produkt.produktBrand,
produkt.produktModel,
sizeMemory.memoryInformation,
(SELECT buyphone.colorValue FROM bluecity.buyphone) AS 'Farve',
(SELECT buyphone.conditionValue FROM bluecity.buyphone) AS 'Stand'
FROM bluecity.produkt
JOIN bluecity.sizememory ON bluecity.produkt.memorySize = bluecity.sizememory.memorySize
JOIN bluecity.color ON buyphone.colorValue = bluecity.color.colorInformation
JOIN bluecity.conditions ON buyphone.conditionValue = bluecity.conditions.conditionInformation

但我似乎无法使联接正确。主表bluecity.produkt需要将其中一些值与其他表连接起来。第一个具有内存大小的连接可以工作,但就是这样。主表应该包含一个Integer Value,如果有意义的话,它将从连接表中获取含义。

如果你能解释为什么以及如何做得更好,那么我可以尝试理解帮助是非常有用的。

添加了创建stmt

CREATE DATABASE bluecity;


CREATE TABLE bluecity.Member 
(memberNumber INTEGER(5) NOT NULL,
firstName VARCHAR(50) NOT NULL,
lastName VARCHAR(25) NOT NULL,
address VARCHAR(40) NOT NULL,
zipNumber INTEGER(4) NOT NULL,
phoneNumber INTEGER(8) NOT NULL,
email VARCHAR(35) NOT NULL,
statusValue INTEGER(1) NOT NULL,
FOREIGN KEY (zipNumber) REFERENCES bluecity.zipCode(zipNumber),
FOREIGN KEY (statusValue) REFERENCES bluecity.ID(statusValue),
PRIMARY KEY (memberNumber));

CREATE TABLE bluecity.ID 
(statusValue INTEGER(1) NOT NULL,
information VARCHAR(19) NOT NULL,
PRIMARY KEY (statusValue))



CREATE TABLE bluecity.zipCode 
(zipNumber INTEGER(4) NOT NULL,
city VARCHAR(32) NOT NULL,
PRIMARY KEY (zipNumber));

CREATE TABLE bluecity.Produkt 
(produktNumber INTEGER(5) NOT NULL,
produktType VARCHAR(30) NOT NULL,
produktBrand VARCHAR(30) NOT NULL,
produktModel VARCHAR(30) NOT NULL,
memorySize INTEGER(2) NOT NULL,
FOREIGN KEY (memorySize) REFERENCES bluecity.sizeMemory(memorySize),
PRIMARY KEY (produktNumber));

CREATE TABLE bluecity.Conditions 
(conditionValue INTEGER(1) NOT NULL,
conditionInformation VARCHAR(13) NOT NULL,
PRIMARY KEY (conditionValue));

CREATE TABLE bluecity.sizeMemory 
(memorySize INTEGER(1) NOT NULL,
 memoryInformation VARCHAR(5) NOT NULL,
 PRIMARY KEY (memorySize));

CREATE TABLE bluecity.Color 
(colorValue INTEGER(2) NOT NULL,
colorInformation VARCHAR(20) NOT NULL,
PRIMARY KEY (colorValue));

CREATE TABLE bluecity.Prices 
(conditionValue INTEGER(1) NOT NULL,
produktNumber INTEGER(5) NOT NULL,
price INTEGER(6) NOT NULL,
FOREIGN KEY (conditionValue) REFERENCES bluecity.Conditions(conditionValue),
FOREIGN KEY (produktNumber) REFERENCES bluecity.Produkt(produktNumber),
PRIMARY KEY (conditionValue, produktNumber));

CREATE TABLE bluecity.buyPhone 
(IMEI Integer(15) NOT NULL,
lagerNummer INTEGER(7) NOT NULL,
produktNumber INTEGER(5) NOT NULL,
colorValue INTEGER(2) NOT NULL,
conditionValue INTEGER(1) NOT NULL,
FOREIGN KEY (produktNumber) REFERENCES bluecity.Produkt(produktNumber),
FOREIGN KEY (colorValue) REFERENCES bluecity.Color(colorValue),
FOREIGN KEY (conditionValue) REFERENCES bluecity.Conditions(conditionValue), PRIMARY KEY (IMEI));

2 个答案:

答案 0 :(得分:0)

您还应该在bluecity.buyphone上进行连接,而不是将其作为选择中的子查询,因为它与您的produkt表相关。 像这样:

Select produkt.produktNumber,
buyphone.lagerNummer AS 'Lagernummer',
produkt.produktBrand,
produkt.produktModel,
sizeMemory.memoryInformation,
buyphone.colorValue AS 'Farve',
buyphone.conditionValue AS 'Stand'
FROM produkt
JOIN buyphone ON buyphone.produktNumber = produkt.produktNumber 
JOIN sizememory ON produkt.memorySize = sizememory.memorySize
JOIN color ON buyphone.colorValue = color.colorInformation
JOIN conditions ON buyphone.conditionValue = conditions.conditionInformation

答案 1 :(得分:0)

        Select DISTINCT produkt.produktNumber AS 'Produkt #',
        buyphone.lagerNummer AS 'Lager #',
        produkt.produktBrand AS 'Mærke',
        produkt.produktModel AS 'Model',
        sizeMemory.memoryInformation 'Hukommelse',
        color.colorInformation AS 'Farve',
        conditions.conditionInformation AS 'Stand',
        prices.price AS 'Pris'
        FROM bluecity.buyphone
        JOIN bluecity.produkt ON bluecity.buyphone.produktNumber = bluecity.produkt.produktNumber
        JOIN bluecity.sizememory ON bluecity.produkt.memorySize = bluecity.sizememory.memorySize
        JOIN bluecity.color ON bluecity.buyphone.colorValue = bluecity.color.colorValue
        JOIN bluecity.conditions ON bluecity.buyphone.conditionValue = bluecity.conditions.conditionValue
        JOIN bluecity.prices ON bluecity.produkt.produktNumber = bluecity.prices.produktNumber
        JOIN bluecity.prices p1 ON bluecity.buyphone.conditionValue = bluecity.prices.conditionValue

我的工作就像这样,大家好。 编辑:我认为现在仍然存在一些缺陷。