2015-05-27 57 views
0

任何人都可以請解釋下面關於結構和語法的兩個問題嗎?mysql中的多選結構

1:爲什麼第一個塊沒問題,但是第二個塊對於重複的列名FD_ID有錯誤1060?我該如何解決它?

SELECT * from(
(SELECT a.FD_ID as id, aa,bb from 
(select FD_ID, NT_VALUE as aa from nutrition_amount where nutrition_amount.NT_ID = 203) as a left join 
(SELECT FD_ID, NT_VALUE as bb from nutrition_amount where nutrition_amount.NT_ID = 204) as b on a.FD_ID = b.FD_ID) as ab left join 
(SELECT FD_ID, NT_VALUE as cc from nutrition_amount WHERE nutrition_amount. NT_ID = 205) AS c on ab. id = c. FD_ID) 


SELECT distinct * from(
(select * from 
(select FD_ID, NT_VALUE as aa from nutrition_amount where nutrition_amount.NT_ID = 203) as a left join 
(select FD_ID, NT_VALUE as bb from nutrition_amount where nutrition_amount.NT_ID = 204) as b on a.FD_ID = b.FD_ID) as ab left join 
(SELECT FD_ID, NT_VALUE as cc from nutrition_amount WHERE nutrition_amount. NT_ID = 205) AS c on ab. id = c. FD_ID) 

2:我試圖建立多個結構到10個不同的FD_ID。這種結構是否可用來做到這一點?見解,請:) /任何更好的解決方案來處理這類問題?

SELECT * FROM (
SELECT * from(
SELECT * FROM (
SELECT * FROM (
SELECT * from(
SELECT * FROM (
SELECT * FROM (
SELECT * from(
(select a.FD_ID as id, aa,bb from 
(select FD_ID, NT_VALUE as aa from nutrition_amount where nutrition_amount.NT_ID = 203) as a left join 
(select FD_ID, NT_VALUE as bb from nutrition_amount where nutrition_amount.NT_ID = 204) as b on a.FD_ID = b.FD_ID) as ab left join 
(SELECT FD_ID, NT_VALUE as cc from nutrition_amount WHERE nutrition_amount. NT_ID = 205) AS c on ab. id = c. FD_ID) AS bc left join 
(SELECT FD_ID, NT_VALUE as dd from nutrition_amount WHERE nutrition_amount. NT_ID = 208) AS d on bc. id = d. FD_ID) AS cd left join 
(SELECT FD_ID, NT_VALUE as ee from nutrition_amount WHERE nutrition_amount. NT_ID = 269) AS e on cd. id = e. FD_ID) AS de left join 
(SELECT FD_ID, NT_VALUE as ff from nutrition_amount WHERE nutrition_amount. NT_ID = 291) AS f on de. id = f. FD_ID) AS ef left join 
(SELECT FD_ID, NT_VALUE as gg from nutrition_amount WHERE nutrition_amount. NT_ID = 303) AS g on ef. id = g. FD_ID) AS fg left join 
(SELECT FD_ID, NT_VALUE as hh from nutrition_amount WHERE nutrition_amount. NT_ID = 307) AS h on fg. id = h. FD_ID) AS hi left join 
(SELECT FD_ID, NT_VALUE as ii from nutrition_amount WHERE nutrition_amount. NT_ID = 605) AS i on hi. id = i. FD_ID) AS ij left join 
(SELECT FD_ID, NT_VALUE as jj from nutrition_amount WHERE nutrition_amount. NT_ID = 606) AS j on ij. id = j. FD_ID) 

給定的錯誤是從1064線7

感謝您的幫助:)

+0

給出所有這些:'FD_ID'別名,問題可能會消失 –

+0

剛剛嘗試過,但沒有奏效。謝謝,雖然:) –

回答

1

只是做

SELECT FD_ID, NT_VALUE 
FROM nutrition_amount 
WHERE NT_ID IN (203, 204, ..., 606); 

和做應用層面的休息,無論出於何種原因你想讓你的數據保持水平。一個數據庫在那裏存儲並以有效的方式獲得數據,沒有別的。這裏有而不是,可以很好地格式化你的數據。如果必須的話,在別處做。

+0

我完全同意你的觀點。 –

+0

但是,我正在處理的數據庫要求我將其水平執行......我嘗試了這個,併爲我的需要提出了一個適用的想法,哈哈:) Thx –

0
SELECT * from (
(select FD_ID as iq, NT_VALUE as aa from nutrition_amount where nutrition_amount.NT_ID = 205) as a left join 
(select FD_ID as iw, NT_VALUE as bb from nutrition_amount where nutrition_amount.NT_ID = 204) as b on a.iq = b.iw left join 
(SELECT FD_ID as ie, NT_VALUE as cc from nutrition_amount WHERE nutrition_amount.NT_ID = 203) AS c on b.iw = c.ie left join 
(SELECT FD_ID as ir, NT_VALUE as dd from nutrition_amount WHERE nutrition_amount.NT_ID = 208) AS d on c.ie = d.ir left join 
(SELECT FD_ID as it, NT_VALUE as ee from nutrition_amount WHERE nutrition_amount.NT_ID = 269) AS e on d.ir = e.it left join 
(SELECT FD_ID as iy, NT_VALUE as ff from nutrition_amount WHERE nutrition_amount.NT_ID = 291) AS f on e.it = f.iy left join 
(SELECT FD_ID as iu, NT_VALUE as gg from nutrition_amount WHERE nutrition_amount.NT_ID = 303) AS g on f.iy = g.iu left join 
(SELECT FD_ID as ii, NT_VALUE as hh from nutrition_amount WHERE nutrition_amount.NT_ID = 307) AS h on g.iu = h.ii left join 
(SELECT FD_ID as ip, NT_VALUE as ii from nutrition_amount WHERE nutrition_amount.NT_ID = 605) AS i on h.ii = i.ip left join 
(SELECT FD_ID as ia, NT_VALUE as jj from nutrition_amount WHERE nutrition_amount.NT_ID = 606) AS j on i.ip = j.ia);