2015-05-27 5 views
0

Может ли кто-нибудь объяснить два вопроса ниже относительно структуры и синтаксиса?Множественная структура выбора в mysql

1: Почему первый блок будет в порядке, а второй имеет ошибку 1060 для двойного имени столбца FD_ID? Как я могу это исправить?

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

 Смежные вопросы

  • Нет связанных вопросов^_^