Hallo Anantha

Join 3 Tables On MySQL

Posted by: Nanta on: August 17, 2007

I got some tasks from my lecture to deploy user complaint form.

I don’t know how to deploy…just try and try. And finally i know how to start. I use PHP and MySQL to deploy it. Generally, I make the form with PHP and html source code. I use 3 tables on MySQL to get Data that i want to show at form. I confuse how to join 3 tables.With help from my friend and GrandPa google, finally I know it. This the topology of that’s 3 tables:

1st table:

+———+————–
| Field | Type
+———+————–
| id | int(10)
| tanggal | varchar(30)
| nama | varchar(100)
| alamat | varchar(200)
| kota | varchar(25)
| email | varchar(150)
| idcom | int(10)
| idwil | int(10)
+———+————–

2nd table:

+——–+————–+
| Field | Type |
+——–+————–+
| idcom | int(10) |
| descom | varchar(100) |
+——–+————–+

3 rd table:

+——–+————–+
| Field | Type |
+——–+————–+
| idwil | int(10) |
| deswil | varchar(100) |
+——–+————–+

from 1 st table , i want to get 2 field idwil and idcom. this two field the same as 2nd table and 3rd table. I want to get deswil from 3rd table and descom from 2nd table. This is the syntax to join both 3 tables:

SELECT tanggal, nama, alamat, email, descom,deswil FROM tugas LEFT JOIN komplain ON tugas.idcom = komplain.idcom LEFT JOIN wilayah ON tugas.idwil = wilayah.idwil ORDER BY id

these will show 3 data from both 3 tables.

Maybe these info help you that have a problem like me..

thank you

-nanta-

7 Responses to "Join 3 Tables On MySQL"

good help

i need a support on how to join 3 to four tables in a database using mysql select but it keeps repeating 1 no throughout..i need the query to do that..like am joining these four tables together now
account payment,account trxn,savings_trxn_details and the relationship among them is the account id but one of them does not have the account id and i want the details on the other to appear on the same table with them..
thanks
andrew

wow,.. great post I should say. Was looking for join and after reading your post and add in some join with your left join, my sql are working!

Again, Thanks = )

Oh, you are the man! It took me a while to google if 3 joins were even possible!

Thanks!!!!!

Thank you so much!! It works for 4 tables too :)

[quote]
$sql=mysql_query(“SELECT title_post, post, id_category, id_topic FROM posting_forum
LEFT JOIN topic_forum ON posting_forum.id_topic = topic_forum.id_topic
LEFT JOIN category_forum ON posting_forum.id_category = category_forum.id_category ORDER BY id_post”);

$x=mysql_fetch_array($sql);

echo “Topik: $x[title_topic]“;
echo “”;
echo “Intro: $x[intro]“;
echo “”;
echo “Category: $x[title_category]“;
echo “”;
echo “Title: $x[title_posting]“;
echo “”;
echo “Post: $x[post]“;
[/quote]

My DataBase:

tbl>1 posting_forum

id_post
id_category
id_topic
title_posting
post

tbl>2 category_forum

id_category
title_category

tbl>3 topic_forum

id_topic
title_topic
intro

———————————————————————————-
I have an error in result :

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\work\showpost.php on line 8

I also rename the fetch_array function to num_rows but still got the same error. Please help me wats wrong with my query or script?

Thank for the help….

I`m sorry, the error was only title_post and post that showed up on the browser. But the title_topic, intro and title_category was blank. Something wrong with my query???

I`m very sorry about the error….

The error was not:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\work\showpost.php on line 8

Leave a Reply

Pages

 

August 2007
M T W T F S S
    Sep »
 12345
6789101112
13141516171819
20212223242526
2728293031  

My !Yahoo

Community

Amateur Radio Club ITB

Balikpapan blogger



KampungBlog.com - Kumpulan Blog-Blog
Indonesia
Personal blogs

Foto2

DSC00009

DSCF0724

CIMG3190

More Photos

Peta-Pengunjung