Ask everyone three table connection query SQL how to write

Source: Internet
Author: User
There are three tables (first behavior field name)

User table:

ID name
1 Aaa
2 Bbb
3 Ccc

Chargerecord Recharge Record Table:

Money
ID userId
1 1 10
2 1 2
3 2 4
4 2 6

Record Call Log table:

ID Callid (main call) Becallid (called) time (length)
1 2 1 23
2 3 1 2
3 2 3 16

How to make a connection using the ID in the user table and the userid of the Recharge record table and the Callid/becallid
Query the user name, recharge the total amount, and the total length of call (call length)
The novice thanked everybody!!

Reply content:

There are three tables (first behavior field name)

User table:

ID name
1 Aaa
2 Bbb
3 Ccc

Chargerecord Recharge Record Table:

Money
ID userId
1 1 10
2 1 2
3 2 4
4 2 6

Record Call Log table:

ID Callid (main call) Becallid (called) time (length)
1 2 1 23
2 3 1 2
3 2 3 16

How to make a connection using the ID in the user table and the userid of the Recharge record table and the Callid/becallid
Query the user name, recharge the total amount, and the total length of call (call length)
The novice thanked everybody!!

SELECT user.id AS id, user.name AS name, charge.money AS money, record.time AS timeFROM userLEFT JOIN (SELECT userId, SUM(money) AS money FROM chargerecord GROUP BY userId) AS charge ON user.id = charge.userIdLEFT JOIN (SELECT callId, SUM(time) AS time FROM record GROUP BY callId) AS record ON user.id = record.callIdORDER BY user.id

How to check upstairs has been given I have an article about multi-table query to the landlord to understand more than one table query helpful: Several connections of SQL: Internal connection, leftist, right connection, full connection, cross connection

If you don't like the answer to the comment, I'll take it back, please don't step on it.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.