MySQL multi-table query data appears serial

Source: Internet
Author: User

As shown, to query a user (from the beginning of the registration) 7 days, 15 days, 30 days of the number of posts, the results of the query data serial.   Because, there are registered users, he did not post 7 days ago, that is, the number of posts 0, in the data table should be shown as NULL, so the data table in 15 days more data, is the user 7 days post number 0. 30 days of the same reason.

How to solve it? First, the maximum period of 30 days of the main reference table , 30 days including 15 days, 7 days, if he is the reference to the main table, the filtered user ID will certainly have 15 days, 7 days, if not posted, the query display is null.

Statement to write like this.

SELECT * FROM (the virtual table generated by the table query 30 days) the left join (the virtual table generated by the query for 7 days) on the connection conditions of the two virtual tables

SELECT * FROM (the virtual table generated by the table query 30 days) the left join (the virtual table generated by the table query 15 days) on the two virtual table join conditions

SELECT
a.ID, a.user_id, a. ' Registration time ', a. ' Number of posts ', B. ' Number of posts '
From
(
SELECT
Qp.id,
qp.user_id,
Qs.wechat,
from_unixtime (qp.create_at, '%y-%m-%d ') registration time,
count (qp.user_id) Number of posts,
count (qs.id) second-hand quantity,
Qs.create_at,
Uw.create_at as Uwcreate
from
Welife_quan.quan_post as QP
Left JOIN Quan_post_second as qs on qs.pid = Qp.id
Left JOIN welife_system.user_wechat as uw on uw.unionid = qp.user_id
GROUP by
qp.user_id
having
qs.wechat! = "
and qs.create_at <= uwcreate + * + *
ORDER by
qp.id DESC
) as A
Left JOIN (
SELECT
Qp.id,
qp.user_id,
Qs.wechat,
from_unixtime (qp.create_at, '%y-%m-%d ') registration time,
count (qp.user_id) Number of posts,
count (qs.id) second-hand quantity,
Qs.create_at,
Uw.create_at as Uwcreate
from
Welife_quan.quan_post as QP
Left JOIN Quan_post_second as qs on qs.pid = Qp.id
Left JOIN welife_system.user_wechat as uw on uw.unionid = qp.user_id
GROUP by
qp.user_id
having
qs.wechat! = "
and qs.create_at <= uwcreate + * 7 *
ORDER by
qp.id DESC
) as B on a.user_id = b.user_id;

MySQL multi-table query data appears serial

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.