我有一個 eg_design
表,其中包含以下幾列:
和 eg_domains
表,其中包含以下列:
和 eg_fonts
表,其中包含以下列:
現在,基於$domain_id 我想取得eg_design
表中的所有資料、eg_domains
表中的一些資料以及eg_fonts
表中的font_name 列值但是在eg_design
表中,我在design_font和domain_font列上存在2個字體ID。
因此,下面的查詢應該從eg_fonts
表中為我提供2 個font_name,就像在eg_design
表中一樣,我存在2 個字體ID。
$get_domain = mysqli_query( $mysqli, "SELECT edg.*, ed.domain_name, egf.* FROM eg_design AS edg LEFT JOIN eg_domains AS ed ON edg.domain_id = ed.domain_id LEFT JOIN eg_fonts AS egf ON egf.font_id = edg.design_font AND egf.font_id = edg.domain_font WHERE edg.domain_id = '$domain_id' ");
但我認為對於這一行
egf.font_id = edg.design_font AND egf.font_id = edg.domain_font
上述查詢不起作用。
我的意思是我可以取得所有數據,但無法取得font_name列值。它只傳回 design_font 列值,而不回傳 domain_font 列值。
你能幫我解決這個問題嗎?
這是小提琴: https://www.db-fiddle.com/f/mNscdKDNohpT3xidp3C9Mw/0
P粉0984172232024-04-07 15:42:37
查看更新的答案
https://www.db-fiddle.com/f/mNscdKDNohpT3xidp3C9Mw/5
SELECT edg.*, ed.domain_name, egf.font_name FROM eg_design edg LEFT JOIN eg_domains ed ON edg.domain_id = ed.domain_id LEFT JOIN eg_fonts egf ON egf.font_title = edg.design_font WHERE edg.domain_id = 1
P粉1166544952024-04-07 09:36:40
基本上,您只需左連接同一個表 eg_fonts
兩次即可。
請參閱 dbfiddle 。您可以嘗試以下查詢:
$get_domain = mysqli_query( $mysqli, "SELECT edg.*, ed.domain_name, egf.font_name AS design_font_name , egf2.font_name AS domain_font_name FROM eg_design AS edg LEFT JOIN eg_domains AS ed ON edg.domain_id = ed.domain_id LEFT JOIN eg_fonts AS egf ON egf.font_id = edg.design_font LEFT JOIN eg_fonts AS egf2 ON egf2.font_id = edg.domain_font WHERE edg.domain_id = '$domain_id' ");
更改此:
LEFT JOIN eg_fonts AS egf ON egf.font_id = edg.design_font AND egf.font_id = edg.domain_font
對此:
LEFT JOIN eg_fonts AS egf ON egf.font_id = edg.design_font LEFT JOIN eg_fonts AS egf2 ON egf2.font_id = edg.domain_font