Database Union table statistics query Group by & INNER JOIN

Source: Internet
Author: User
Tags joins

Original Data Sheet

Video Information Sheet Tab_video_info

Play record table Tab_play_record

Demand

Count the top 20 videos that have been played (have already been played);

 select  a.video_id, sum  (' Is_play ') Span style= "COLOR: #0000ff" >as   total  from  ' tab_video_info ' as  a inner  join  ' Tab_play_record ' as  b on  a.video_id =   b.video_id  group  by   a.video_id  order  by  total desc  limit  0 , 40 ;

Statistical results

INNER JOIN Syntax :

INNER join joins two data table usage:
SELECT * FROM table 1 INNER JOIN table 2 on table 1. Field number = Table 2. Field number

INNER join joins three data table usage:
SELECT * FROM (table 1 INNER join table 2 on table 1. Field number = Table 2. Field number) INNER JOIN table 3 on table 1. Field number = Table 3. Field number

INNER join joins four data table usage:
SELECT * FROM (table 1 INNER join table 2 on table 1. Field number = Table 2. Field number) INNER join table 3 on table 1. Field number = Table 3. Field number) INNER join table 4 on Member. Field number = Table 4. Field number

INNER join joins five data table usage:
SELECT * FROM (table 1 INNER join table 2 on table 1. Field number = Table 2. Field number) INNER join table 3 on table 1. Field number = Table 3. Field number) INNER join table 4 on Member. Field number = Table 4. Field number) in NER JOIN table 5 on Member. Field number = Table 5. Field number

Connection six data Tables usage: slightly, similar to the above join method, everybody extrapolate bar:)

Precautions:
In the process of entering letters, be sure to use the English half-width punctuation marks, between the words left half the space between the corners;
When you create a data table, if a table joins multiple tables, the fields in this table must be numeric data types, and the same fields in multiple tables must be primary keys and AutoNumber data types. Otherwise, it is difficult to join successfully.
Code nesting Quick Method: For example, if you want to connect five tables, just add a front and back bracket to the code that joins the four tables (the parentheses are appended to the end of the code), and then continue adding "INNER JOIN table name x on table 1 after the parentheses." Field number = table X. Field number " Code so that you can join the data table indefinitely.

INNER JOIN (equivalent connection) returns only rows that have the same join field in two tables
Left join (left join) returns records that include all records in the left table and the equivalent of the junction fields in the right table
Right join (right Join) returns records that include all records in the right table and the equivalent of the junction fields in the left table

Group by usage

"GROUP BY" is the literal understanding that the data is grouped according to the rules specified by the "by", in which the so-called grouping is dividing a "dataset" into several "small regions" and then processing data for several small regions.

Usage Description: http://www.cnblogs.com/rainman/archive/2013/05/01/3053703.html (this blog is more specific, recommended)

Database Union table statistics query Group by & INNER JOIN

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.