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