Foreword
Always believe that good is about to happen
background
Some time ago come across such a problem, check out a shop in 2020 and 2019 events of type 'xxx' date, due to historical reasons, the 2019 activities and 2020 events are stored in two tables, required by foreign the results show the connection put together, the end result to be probably a long way:
2019 Date | 2019 Events | 2020 Date | 2020 Events |
---|---|---|---|
2019/01/05 | Section xxx | 2020/02/01 | Section xxx |
2019/03/12 | Section xxx | 2020/04/10 | Section xxx |
2019/07/19 | Section xxx | 2020/06/05 | Section xxx |
2020/09/09 | Section xxx | ||
2020/12/15 | Section xxx |
Stepped pit
Just took over the needs of the time, my heart is so
I resorted to my primary school grade 5 SQL skills, by Fermat's theorem, Taylor's formula, Lagrange's theorem and the Hospital's Rule, combined with my dazzling one finger typing skills, and finally live up to expectations did not write out
What is it cause of the problem, a separate inquiry for the 2019 event type 'xxx' date is very simple, single query type of activity in 2020 as 'xxx' date is also very simple, the conditions associated with it? There will certainly be daft to say, ah, ah or associated with the activities associated with the store id type
Search Result Search Result of left and right have been check out of the shop respectively in 2020 and 2019 events of type 'xxx' the date, whether it is done by the shop id or type of activity will be associated with a Cartesian product , so the need for another to find other related conditions
solve
Holding hesitate to ask, no, is disgraced asked on learning attitude, ask our boss, the scene was something like this
Big brother showed me the Ming Road, the line number associated with the query, and then personally guided a lot, in the splendor of Daniel and my tireless efforts (bai) force (du), I finally live up to expectations written out, after the SQL substantially simplified as follows:
SELECT
*
FROM
(
SELECT
@rownuma := @rownuma + 1 AS num_1, table_a.*
FROM table_a,
( SELECT @rownuma := 0 )
) a
LEFT JOIN (
SELECT
@rownumb := @rownumb + 1 AS num_2, table_b.*
FROM table_b,
( SELECT @rownumb := 0 )
) b ON a.num_1 = b.num_2
The results of the query results to the query on the left and right, respectively, to add a line number, line number are from 1 to start, so that you can do on the left data sets and data sets on the right of association, but
When the number of rows in the result set is less than or equal to the left to the right of the results, such an approach does not have any problem, then the result when the right set greater than the number of rows to the left of it, yes, and it is the same as the number of rows to the left of the display section only, so the question is, when the number of result set rows uncertain about how to do it? Yes, use the union connections at the left outer join and right outer join result set on the k-o
SELECT
*
FROM
(
SELECT
@rownuma := @rownuma + 1 AS num_1, table_a.*
FROM table_a,
( SELECT @rownuma := 0 )
) a
LEFT JOIN (
SELECT
@rownumb := @rownumb + 1 AS num_2, table_b.*
FROM table_b,
( SELECT @rownumb := 0 )
) b ON a.num_1 = b.num_2
UNION
SELECT
*
FROM
(
SELECT
@rownumc := @rownumc + 1 AS num_1, table_a.*
FROM table_a,
( SELECT @rownumc := 0 )
) a
RIGHT JOIN (
SELECT
@rownumd := @rownumd + 1 AS num_2, table_b.*
FROM table_b,
( SELECT @rownumd := 0 )
) b ON a.num_1 = b.num_2
important point
Use line numbers when remember not to use the name of the same name, should be like this
If you use the same name, you will find that the result set associated with a column after the start will be a lot of blank, do not ask me why I know that there is a pit Han Han and it was a long time ago do not know the reason
to sum up
This is similar to the actual development encountered no rules associated with the scene in fact relatively small, but because of the lack of a lot of MySQL SQL associated method, has led to something else need to think about methods to solve, I hope you read my step on pit tour after less able to explore a mine.
And encourage each party of