Summary of Leetcode-Mysql topics and knowledge points (197. Rising temperature)

Computer Xiaobai QAQ, because I want to find a few summer internships, I have filled the members and want to focus on the mysql part of leetcode. Write this series of blog posts to communicate with you, and we will continue to update some of the prepared questions in the future. Welcome to communicate with you, and ask the big guys to spray QAQ. Because I have taken many detours as a beginner, I will try my best to write in detail. If you can help future friends, please leave a message to encourage me. Hahahaha.

Chapter 197: Rising Temperature

I prefer to brush off questions because the output results can be adjusted to the expected results a little bit, but Niuke.com is more like a scene when we are applying for a job without submitting the output. In the final analysis, it is still a good QAQ.

Knowledge point 1: Cartesian product connection method, before we mentioned left connection right connection and inner connection full connection, here we introduce a new connection method. The same example is the same. If the id of table 1 is 1, 2, 3, and the id of table 2 is 2, 3, 4. When using Cartesian product connection, the id of table 1 will be combined with each id of table 2 to form a new The two columns of the table, regardless of whether the two are equal, that is to say, in the above example, the id of the two tables will produce a total of nine rows, (1,2), (1,3), (1,4), etc., Therefore, the table formed by the Cartesian product has a higher dimension than the ordinary join. The specific construction process is from Table 1, Table 2

Knowledge point 2: DATEDIFF(date1, date2) returns the number of days between the start time date1 and the end time date2. date1 and date2 are dates or date-and-time expressions

Note 1: In sql, a single equal sign "=" is used directly instead of the double equal sign "==" commonly used in python

Note 2: Regardless of the connection method, you need to pay attention to the two tables with the same column name, you must specify which table is the column, table n. column name. And not directly write the column names vaguely.

Idea: Because it is easier to compare left and right than up and down for SQL, I want to treat the table as two first and then connect left and right, but because the two tables are exactly the same, if you use the connection method mentioned earlier It is equivalent to that there is no more information than the previous table, so I want to use a new connection method called Cartesian product to connect the two tables, and at the same time filter out the difference between the two dates by one day and the temperature of the next day is higher The id of the previous day is returned.

Code:

select w1.id from Weather as w1,Weather as w2 where datediff(w1.recordDate,w2.recordDate)=1 and w1.Temperature>w2.Temperature

Guess you like

Origin blog.csdn.net/weixin_43167461/article/details/113184080