2015-12-09 33 views
0

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需要將其某些值與其他表連接起來。內存大小的第一次連接起作用,但就是這樣。如果有意義的話,主表應該保存一個整數值,這個整數值可以從連接表中提取出意義。

幫助是非常appriciated,如果你能解釋爲什麼以及如何更好,所以我可以嘗試理解。

新增創建語句

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)); 
+0

子查詢(大括號中的那個)返回多少個記錄? – 0xCAFEBABE

+0

嗯,我認爲它只返回一個。 –

+0

你使用多個數據庫嗎?否則,爲什麼kepp提到數據庫名稱。另外,你的問題還不清楚。如果您願意,可以考慮遵循以下簡單的兩步操作步驟:1.如果您尚未這樣做,請提供適當的CREATE和INSERT語句(和/或sqlfiddle),以便我們可以更輕鬆地複製問題。 2.如果您尚未這樣做,請提供與步驟1中提供的信息相對應的所需結果集。 – Strawberry

回答

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 

我得到了它這樣工作,THX的傢伙。 編輯:我認爲現在仍然存在一些缺陷。

+0

這點沒有意義加入bluecity.buyphone ON bluecity.buyphone.lagerNummer = bluecity.buyphone.lagerNummer JOIN bluecity.buyphone u2 ON bluecity.buyphone.colorValue = bluecity.buyphone.colorValue JOIN bluecity.buyphone u3 ON bluecity.buyphone.conditionValue = bluecity.buyphone.conditionValue你沒有加入外鍵 嘗試添加更多的條目購買電話表,它不會工作 – HansP

+0

你是對的,我發現我需要使用表buyphone作爲主表。一切正常,因爲它應該是這樣的,儘管可能還是有問題。 –

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

我試過這個,但是我不能把它加入探測,我不知道我應該加入它嗎? –

+0

接近我所需要的,雖然你給我的工具來通過它非常感謝。 –

+0

你能解釋一下你正在嘗試做什麼嗎? – HansP