繁体   English   中英

尝试加入多个表

[英]Try to join more than one table

表格以及我需要的东西我正在尝试为一个学校项目创建一个带有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需要将其某些值与其他表联接。 第一次使用内存大小的连接有效,但仅此而已。 应该在主表中保存一个整数值,如果有意义的话,该值将从联接表中提取含义。

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

添加了创建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));

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

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
        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

伙计们,我像这样工作。 编辑:我认为现在仍然存在一些缺陷,现在可以修复。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM