Re: [SQL ] 交叉查詢
感謝 twoseven板友的協助 我對於 mysql的 join用法友比較瞭解了一點
原來在 mysql內如果用了兩個join以上的話 就要把所有連接的表格都要做alias.
以下是經過twoseven協助後 寫出的語法
SELECT a2.`cate_parent_id`, c.cate_name AS cate_name_parent ,
a2.`cate_child_id` , a1.`cate_name` AS cate_name,
a1.`cate_description` , a1.`cate_publish`
FROM `cate_xref` a2
LEFT JOIN `cate` a1 ON ( a2.`cate_child_id` = a1.`cate_id` )
LEFT JOIN `cate` c ON ( a2.`cate_parent_id` = c.`cate_id` )
ORDER BY `cate_parent_id`
經過協助 我也去查了一下mysql的手冊 總算解決了問題
感謝各位大俠
※ 引述《yihinwind (跑很快的驢子)》之銘言:
: 請教各位大俠,我碰到了一個狀況,不知道該如何解決
: 目前有兩張表分別為
: cate(存放所有類別敘述) 和
: 欄位內容為
: cate_id cate_name cate_description cate_thumb cate_publish
: 0 abc abcdesc athumb Y
: 1 def defdesc dthumb n
: 2 ghi ghidesc gthumb Y
: 3 jk jkdesc jthumb Y
: 4 l ldesc lthumb Y
: 5 mno mdesc mthumb Y
: cate_xref(存放類別之間的繼承關係)
: 欄位內容為
: cate_parent_id cate_child_id
: 0 1
: 0 2
: 1 3
: 3 4
: 2 5
: 用了LEFT JOIN後採下列語法
: SELECT `cate_xref`.`cate_parent_id` ,
: `cate_xref`.`cate_child_id` ,
: `cate`.`cate_name` , `category` AS cate_name_child.`cate_description`
: , `cate`.`cate_publish`
: FROM `cate_xref`
: LEFT JOIN `cate` ON `cate_xref`.`cate_child_id` =
: `cate`.`cate_id`
: ORDER BY `cate_parent_id`
: 產生這樣的表格
: cate_parent_id cate_child_id cate_name_child cate_description cate_publish
: 0 1 abc
: 0 2 恕刪
: 3 4
: 想要請教的是 如果我想要在cate_parent_id 和 cate_child_id之間
: 多交叉查詢出cate_parent_id所對應的 cate_name(假定叫做cate_name_parent)
: 不知道 我的Sql語法 要做甚麼樣子的調整呢?
: (不知道該怎麼樣才能夠 將兩個資料同時都對應到同樣的cate_name欄位)
: 麻煩大家了 謝謝
: PS. 我用mysql
--
內有大隻佬 請小心餵食
http://www.wretch.cc/album/fernandolee
http://www.wretch.cc/album/yihinwind
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 122.117.230.116
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):