473,396 Members | 1,916 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Sql For Join 3 Tables

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.
Aug 21 '07 #1
6 5170
Atli
5,058 Expert 4TB
Hi, and welcome to TSDN!

What type of SQL server are you using?
Have you made any attempts at this yourself?
Aug 22 '07 #2
jx2
228 100+
i think you need to read this

regards
jx2
Aug 22 '07 #3
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
Aug 22 '07 #4
Atli
5,058 Expert 4TB
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.  
Aug 22 '07 #5
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.
Aug 22 '07 #6
Atli
5,058 Expert 4TB
Why are you using SUM() for the invoice?
Try removing the SUM() function and just return 'table 2.invoice'.
Aug 22 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Daniel Rossi | last post by:
Hi there i am trying to work out the most efficient way to list say multipl= e categories of entries, the database is quite large about 200 meg.=20 I would like to know if using join tables is...
3
by: Steve Bishop | last post by:
I am use to working with only single tables with my command object. Now I have 2 tables that need a 1 to 1 relationship. Is it possible to inner join 2 tables in my SQL statement before thowing the...
4
by: Mark | last post by:
hi. i have two tables. table one ("users") contains two columns, "user_id", and "user_name" table two ("spam") contains three columns "msg_id","user_id", and "msg" basically, i want to...
2
by: zwasdl | last post by:
I'm using access to connect to Oracle via ODBC. I can also connect to Oracle via sql*plus. Can I write a query to join tables from different schema? If so, how? Thanks a million! Wei
2
by: =?Utf-8?B?VGVycnk=?= | last post by:
Is it possible to join tables from different databases? If so, what is the format of the select statement? Both databases are on the same server. TIA, -- Terry
21
patjones
by: patjones | last post by:
Hi all: My newest project involves creating a small help desk database for our customer service division to use. What will happen is that an employee will call up, explain his/her issue, and...
5
Bob Ross
by: Bob Ross | last post by:
Is there a way to to create a single table from multiple tables in a dataset? I currently have a dataset with multiple tables and and relations connecting those tables. Table1 has many records to...
0
by: fantabk | last post by:
Hello, I have 2 tables that I need to join and update, the query look like bellow: UPDATE M1L INNER JOIN M1 ON (M1L.A1 = M1.A2) SET M1L.A3 = . WHERE xxx; In M1, we have multiple records...
7
by: gerryis2000 | last post by:
Hi, i have two tables that i wish to join. from the items table and purchases table, i use a query to list all the purchases for the day. then using sum i get all the amount used to purchase.my...
5
by: Amit Kumar M | last post by:
Which one is better way to join tables in SQL ? SELECT * FROM TABLE1, TABLE2 WHERE TABLE1.KEY = TABLE2.VALUE OR SELECT * FROM TABLE1 INNER JOIN TABLE2 ON TABLE1.KEY = TABLE2.VALUE Although...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.