SQL multi-Table query

Source: Internet
Author: User

SQL multi-Table query

Select distinct (. name, B. name), COUNT (Electric Appliance sales revenue + special monthly sales revenue) as monse, COUNT (Electric Appliance sales revenue + special annual sales revenue) as year FROM a, B WHERE. id = B. id;

There are two tables:
Table
Name Electric Appliance monthly sales revenue Electric Appliance annual sales revenue
Zhang San 5000 200000
Li Si 6000 30000
Wang Wu 7000 700000
Table B
Name special month sales revenue special year sales revenue
Ma 2 3000 36000
Zhang San 2000 24000
Liu Qi 2500 30000
Wang Wu 1600 19200
I want to get everyone's total revenue: How to Write PHP? Thank you!
Total Monthly Income (Electric Appliance sales revenue + special monthly sales revenue) annual income (Electric Appliance sales revenue + special annual sales revenue)
Zhang San
Li Si
Wang Wu
Ma 'er
Liu Qi

Select name, sum (Electric Appliance sales revenue) as total monthly income, sum (Electric Appliance sales revenue) as annual income from (SELECT * from a table union SELECT * from B Table
) C group by name;

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.