[PHP]ใช้ left join แล้ว field มันซ้ำกันแก้ยังไงค่ะ

เริ่มโดย SG14, 02 พฤษภาคม 2012, 15:30:32

หัวข้อก่อนหน้า - หัวข้อถัดไป

0 สมาชิก และ 1 ผู้มาเยือน กำลังดูหัวข้อนี้

SG14

ใช้ left join แล้ว field มันซ้ำกันแก้ยังไงค่ะ

table1
- ID
- NAME

table2
- ID
- NAME

คือใช้ left join table1 กับ table2
แล้วfield มันชื่อซ้ำกัน
จะ <?=$data['name']?> ในรูปแบบนี้อะคะ

พอ echo ออกมาผลคือ มัน echo name ของ table2 เท่านั้นค่ะ ไม่รู้จะให้ echo name ของtable1 ยังไง
:wanwan017:


gilbert

#2
SELECT  t1.NAME AS NAME1 , t2.NAME AS NAME2
FROM table1 t1
LEFT JOIN table2 t2 ON t1.ID = t2.ID

<?=$data['NAME1']?>
<?=$data['NAME2']?> 

ตามข้างบนครับ

somsak_tk

เวลาเรียก field ให้ใส่ชื่อกำกับ ชื่อ table ไปด้วย เช่น

select  A.id, A.name, B.id, B.name
from table1 A, table2 B


อันนี้เป็นมาตรฐาน SQL ไม่ว่าsql ยี่ห้ออะไรก็ใช้ได้คับ table1 A คำว่า A จะกลายเป็นตัวย่อ
เมื่อต้องการเรียก table นั้นๆแทน จากเดิมที่เรียก table1.id ก็เรียกแค่ A.id ได้
$100 ต่อเดือน Completed แบบง่ายสุดๆด้วย Script ที่ซื้อมาแล้วจัดแต่งมั่วๆซั่วๆ
เป้าหมายต่อไป $500 ต่อเดือน

xvlnw.com

อ้างถึงจาก: gilbert ใน 02 พฤษภาคม 2012, 15:45:07
SELECT  t1.NAME AS NAME1 , t2.NAME AS NAME2
FROM table1 t1
LEFT JOIN table2 t2 ON t1.ID = t2.ID

<?=$data['NAME1']?>
<?=$data['NAME2']?> 

ตามข้างบนครับ

ตามนี้เลยครับ Confirm...
[direct=https://cloudhost.in.th/wordpress-hosting]ツ ⓌⓄⓇⒹⓅⓇⒺⓈⓈ ⒽⓄⓈⓉⒾⓃⒼ [/direct] :wanwan014:  :D
[direct=https://cloudhost.in.th/cloudhosting.html]Cloud Hosting[/direct] [direct=https://cloudhost.in.th/cloudvps.html]Cloud Server[/direct] [direct=https://cloudhost.in.th/vpshosting.html]Cloud VPS Hosting[/direct] [direct=https://cloudhost.in.th/windowsvps.html]Cloud Windows[/direct] Tel: 080-348-0843 LINE: ixvlnw