472,123 Members | 1,373 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

how to view with 3 table

I have 3 tables
1. dealer(id,phone) --- primary key(id)
2. payment(id,amount) --- foreign key(id) reference dealer(id)
3.sell(id,unit) --- foreign key(id) reference dealer(id)


The dealer table contains----------
id---phone
______________________
1-------11
2-------22
3-------33
4-------44
5-------55

The payment table contains--------
id amount
_______________________
1-------100
2-------200
3-------300
2-------200
3-------300
2-------200


The sell table contains--------
id unit
_______________________
3-------3
4-------4
3-------3
3-------3



I want to make such a sql that return like the following---------

id amount unit
____________________________
1-------100-------0
2-------400-------0
3-------900-------9
4-------0----------4
5-------0--------- 0

Please help me to make the sql.
Sep 4 '07 #1
1 950
pbmods
5,821 Expert 4TB
Heya, Hasan. Welcome to TSDN!

To do this, you'll want to join two of your tables, probably using a LEFT JOIN; that would make for the most readable code.

You'll also need to use the SUM() aggregate function, which also means that you'll need to GROUP BY `id`.

This should get you started. You're looking at a real meat-and-potatoes situation here. Once you get used to writing this sort of query, it makes 90% of the rest of your job a whole lot easier (or at least the part of it that involves writing queries).
Sep 4 '07 #2

Post your reply

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

Similar topics

2 posts views Thread by WertmanTheMad | last post: by
3 posts views Thread by brendan_gallagher_2001 | last post: by
11 posts views Thread by Paul Reddin | last post: by
12 posts views Thread by Raed Sawalha | last post: by
4 posts views Thread by Nathan Sokalski | last post: by
reply views Thread by leo001 | last post: by

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.