2012-11-15 52 views
0

我遇到了一個簡單查詢的問題。那就是:Mysql - ON子句中的未知列

SELECT `T_COURS`.`COU_DATE`, 
`T_SESSIONS`.*, 
`T_FORMATIONS`.`FOR_TITRE`, `T_FORMATIONS`.`FOR_ID`, `T_FORMATIONS`.`FOR_TITRE`,  `T_FORMATIONS`.`FOR_CATEGORIE`, 
`T_FORMATIONS`.`FOR_DESCRIPTION`, `T_FORMATIONS`.`FOR_MIN_PART`, `T_FORMATIONS`.`FOR_MAX_PART`, `T_FORMATIONS`.`FOR_PRIX`, `T_FORMATIONS`.`FOR_LANGUE` 
FROM `T_FORMATIONS` 
INNER JOIN `T_COURS` ON `T_SESSIONS`.`SES_ID` = `T_COURS`.`COU_SES_ID` 
INNER JOIN `T_SESSIONS` ON `T_FORMATIONS`.`FOR_ID` = `T_SESSIONS`.`SES_FOR_ID` 
WHERE `T_SESSIONS`.`SES_ETAT` = 1 
ORDER BY `T_COURS`.`COU_DATE` ASC, `T_SESSIONS`.`SES_TITRE` ASC, `T_FORMATIONS`.`FOR_TITRE` ASC LIMIT 5 

當我嘗試運行它,我得到這個消息:

#1054 - Unknown column 'T_SESSIONS.SES_ID' in 'on clause' 

我真的不明白爲什麼,因爲一切都在我的數據庫中是否存在?

我在網上尋找答案,但它沒有幫助我(或者我不明白我讀的答案)。

+2

郵政數據庫方案,則請。 – Sirko

+0

我想你有兩個連接交換。在解析「T_COURS」行時,「T_SESSIONS」不存在。 –

+1

將「INNER JOIN'T_SESSIONS」放在'T_FORMATIONS'.'FOR_ID' ='T_SESSIONS'.'SES_FOR_ID'「前面」INNER JOIN'T_COURS' ON'T_SESSIONS'.'SES_ID' ='T_COURS'.'​​'COU_SES_ID'「 –

回答

0

沒有看到您的全表結構,我會開始交換您的訂單JOIN,您在嘗試在當前版本中使用它時加入T_SESSIONS。您目前有:

FROM `T_FORMATIONS` 
INNER JOIN `T_COURS` 
    ON `T_SESSIONS`.`SES_ID` = `T_COURS`.`COU_SES_ID` 
INNER JOIN `T_SESSIONS` 
    ON `T_FORMATIONS`.`FOR_ID` = `T_SESSIONS`.`SES_FOR_ID` 

將其更改爲:

FROM `T_FORMATIONS` 
INNER JOIN `T_SESSIONS` 
    ON `T_FORMATIONS`.`FOR_ID` = `T_SESSIONS`.`SES_FOR_ID` 
INNER JOIN `T_COURS` 
    ON `T_SESSIONS`.`SES_ID` = `T_COURS`.`COU_SES_ID` 

製作完整的查詢:

SELECT `T_COURS`.`COU_DATE`, 
    `T_SESSIONS`.*, 
    `T_FORMATIONS`.`FOR_TITRE`, 
    `T_FORMATIONS`.`FOR_ID`, 
    `T_FORMATIONS`.`FOR_TITRE`,  
    `T_FORMATIONS`.`FOR_CATEGORIE`, 
    `T_FORMATIONS`.`FOR_DESCRIPTION`, 
    `T_FORMATIONS`.`FOR_MIN_PART`, 
    `T_FORMATIONS`.`FOR_MAX_PART`, 
    `T_FORMATIONS`.`FOR_PRIX`, 
    `T_FORMATIONS`.`FOR_LANGUE` 
FROM `T_FORMATIONS` 
INNER JOIN `T_SESSIONS` 
    ON `T_FORMATIONS`.`FOR_ID` = `T_SESSIONS`.`SES_FOR_ID` 
INNER JOIN `T_COURS` 
    ON `T_SESSIONS`.`SES_ID` = `T_COURS`.`COU_SES_ID` 
WHERE `T_SESSIONS`.`SES_ETAT` = 1 
ORDER BY `T_COURS`.`COU_DATE` ASC, `T_SESSIONS`.`SES_TITRE` ASC, `T_FORMATIONS`.`FOR_TITRE` ASC 
LIMIT 5 
+0

太棒了!非常感謝你!你救了我的命! :D – LancelotKiin

+0

@LancelotKiin樂意幫忙。真高興你做到了。 – Taryn

相關問題