Connecting Tech Pros Worldwide Forums | Help | Site Map

Sql For Join 3 Tables

Newbie
 
Join Date: Aug 2007
Posts: 3
#1: Aug 22 '07
I have three tables :

table 1 : Id,Name Example : (1 record): 1 , TOTO
table 2 : id,invoice Example : (1 record): 1 ,10 000.00
table 3 : id,payment Example : (2 records): 1 , 5 000.00
1, 2 500.00

What is the sql that i can have one line with the result as folows:

id Name Invoice Payment
1 TOTO 10 000.00 7 500.00

Thanks.

Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,754
#2: Aug 22 '07

re: Sql For Join 3 Tables


Hi, and welcome to TSDN!

What type of SQL server are you using?
Have you made any attempts at this yourself?
jx2 jx2 is offline
Familiar Sight
 
Join Date: Feb 2007
Location: Bristol UK
Posts: 227
#3: Aug 22 '07

re: Sql For Join 3 Tables


i think you need to read this

regards
jx2
Newbie
 
Join Date: Aug 2007
Posts: 3
#4: Aug 22 '07

re: Sql For Join 3 Tables


Quote:

Originally Posted by Atli

Hi, and welcome to TSDN!

What type of SQL server are you using?
Have you made any attempts at this yourself?

Mysql php

Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2.   table 1.id,
  3.   table 1.name,
  4.   sum(table 2.invoice),
  5.   sum(table 3 payment)
  6. FROM 
  7.   table 1,table 2,table 3 
  8. WHERE  table 1.id= table 2.id 
  9. and  table 1.id= table 3.id   
  10. GROUP by  table 1.id
  11.  
The result is always the double amount of INVOICE ( SUM amount =
20 000.00
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,754
#5: Aug 22 '07

re: Sql For Join 3 Tables


Quote:

Originally Posted by zarqane

Mysql php

Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2.   table 1.id,
  3.   table 1.name,
  4.   sum(table 2.invoice),
  5.   sum(table 3 payment)
  6. FROM 
  7.   table 1,table 2,table 3 
  8. WHERE  table 1.id= table 2.id 
  9. and  table 1.id= table 3.id   
  10. GROUP by  table 1.id
  11.  
The result is always the double amount of INVOICE ( SUM amount =
20 000.00

This is because you are not defining which columns the tables are sharing, which results in duplicate rows.

I recommend you read the link jx2 posted above, and then try doing something like this:
Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2.   table1.id,
  3.   table1.name,
  4.   sum(table2.invoice),
  5.   sum(table3 payment)
  6. FROM 
  7.   table1
  8. INNER JOIN table2
  9.   ON table2.id = table1.id
  10. INNER JOIN table3
  11.   ON table3.id = table1.id
  12. GROUP by  table1.id
  13.  
Newbie
 
Join Date: Aug 2007
Posts: 3
#6: Aug 22 '07

re: Sql For Join 3 Tables


Quote:

Originally Posted by Atli

This is because you are not defining which columns the tables are sharing, which results in duplicate rows.

I recommend you read the link jx2 posted above, and then try doing something like this:

Expand|Select|Wrap|Line Numbers
  1. SELECT 
  2.   table1.id,
  3.   table1.name,
  4.   sum(table2.invoice),
  5.   sum(table3 payment)
  6. FROM 
  7.   table1
  8. INNER JOIN table2
  9.   ON table2.id = table1.id
  10. INNER JOIN table3
  11.   ON table3.id = table1.id
  12. GROUP by  table1.id
  13.  

I have trying left join and inner join as you recommended but the result continue the same as before.

B.REGARDS.
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,754
#7: Aug 22 '07

re: Sql For Join 3 Tables


Why are you using SUM() for the invoice?
Try removing the SUM() function and just return 'table 2.invoice'.
Reply