Jump to content

Getting data from multiple tables


aztec

Recommended Posts

Hello

 

I am using PHP with a MySQL database.

 

I have multiple tables in the database each with the same column names.

 

There is a FID column which contains a unique number across all the database.

 

The data in each row contains a link to the table above, i.e. table9 is linked to table10 etc.

 

At this time I am using three queries to get data from three different tables.

 

Question 1:- how do I start to construct a query that will get data from three tables given that the column names for each table are the same.

 

Question 2:- is it more economical on the server to run one complex query than three small queries.

 

Kind Regards

Link to comment
Share on other sites

Hello

 

All of the fields are common fields, the content of FID is unique across the database. In each table there is a link to FID from the table number below, ie table 9 has a link to the FID of table 10 and table 10 has a link to table 11.

 

Regards

Link to comment
Share on other sites

First thing can we please see the tables from the database please...

 

post your database structure please...........

 

using a join is defently faster then using three seprate database querys........

 

Saying that if the database is a stand alone database run on it own server,then there be

no real diffrence to the preformance off the database server, but it will diffently make

your life easer as a programmer to use joins in this current project.........

 

 

Link to comment
Share on other sites

Hello

 

This is my table structure and is common acros the database

 

id is auto increment and is table based.

fid is unique number across the database

 

id  	int(11)
Fid  	int(10)
first_name  	varchar(255)
second_name  	varchar(255)
surname  	varchar(255)
mf  	text
dob  	date
pob  	varchar(40)
dod  	date
pod  	varchar(40)
burial  	varchar(40)
dom  	date
spouses  	int(4)
link_to_spouse
link_to_fid  	int(4)

 

 

Regards

Link to comment
Share on other sites

Hello

 

The table names start at gen_5 and repeat up to gen_12.  Each table holds the data of a generation of family members.

 

So at this time there are 8 tables in the database each the same structure but each holding different data

 

Regards

Link to comment
Share on other sites

this might help you...

"select ti.fieldname,t2.fieldname,t3.fieldname from t1 left join t2 on t.id=t2.id left join t3 on t2.id=t3.id"

 

 

here id is common fieldname in three tables and have commom values.....

Link to comment
Share on other sites

Here a join off three tables ok

 

you add the next 5 lol good luck.........

 

<?php

$sql="SELECT
table1.id, 
table1.Fid, 
table1.first_name,  	
table1.second_name,  
table1.surname,  	
table1.mf,  	
table1.dob,  	
table1.pob,  	
table1.dod,  	
table1.pod,  	
table1.burial,  	
table1.dom,  	
table1.spouses,  	
table1.link_to_spouse,
table1.link_to_fid as one,
table2.id, 
table2.Fid, 
table2.first_name,  	
table2.second_name,  
table2.surname,  	
table2.mf,  	
table2.dob,  	
table2.pob,  	
table2.dod,  	
table2.pod,  	
table2.burial,  	
table2.dom,  	
table2.spouses,  	
table2.link_to_spouse,
table2.link_to_fid as two,
table3.id, 
table3.Fid, 
table3.first_name,  	
table3.second_name,  
table3.surname,  	
table3.mf,  	
table3.dob,  	
table3.pob, 	
table3.dod,  	
table3.pod,  	
table3.burial,  	
table3.dom,  	
table3.spouses,  	
table3.link_to_spouse,
table3.link_to_fid as three,
FROM one,two,three
WHERE one.Fid=two.Fid AND two.Fid=three.Fid
"; 
$sql_result=mysql_query($sql)or die(mysql_error()); 	
?>

Link to comment
Share on other sites

zenag would this work aswell

 

<?php

$sql="SELECT
table1.id, 
table1.Fid, 
table1.first_name,  	
table1.second_name,  
table1.surname,  	
table1.mf,  	
table1.dob,  	
table1.pob,  	
table1.dod,  	
table1.pod,  	
table1.burial,  	
table1.dom,  	
table1.spouses,  	
table1.link_to_spouse,
table1.link_to_fid as one,
table2.id, 
table2.Fid, 
table2.first_name,  	
table2.second_name,  
table2.surname,  	
table2.mf,  	
table2.dob,  	
table2.pob,  	
table2.dod,  	
table2.pod,  	
table2.burial,  	
table2.dom,  	
table2.spouses,  	
table2.link_to_spouse,
table2.link_to_fid as two,
table3.id, 
table3.Fid, 
table3.first_name,  	
table3.second_name,  
table3.surname,  	
table3.mf,  	
table3.dob,  	
table3.pob, 	
table3.dod,  	
table3.pod,  	
table3.burial,  	
table3.dom,  	
table3.spouses,  	
table3.link_to_spouse,
table3.link_to_fid as three,
FROM one,two,three
WHERE one.Fid=two.Fid AND two.Fid=three.Fid
"; 
$sql_result=mysql_query($sql)or die(mysql_error()); 	
?>

Link to comment
Share on other sites

Hello

 

Using the code provided by redarrow I get the following error message:-

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM one,two,three WHERE one.Fid=two.Fid AND two.Fid=three.Fid'

 

I have only changed the table1, table2 and table3 to the actual table name in the database.

 

Regards

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.