当前位置:网站首页>How to analyze fans' interests?
How to analyze fans' interests?
2022-07-28 04:59:00 【Monkey data analysis】
【 subject 】
There is a “ Fan attention table ”, contain 3 A field : user id、 Follow the media id、 date .
【 problem 】“ Fan attention table ” There is a situation that one user pays attention to multiple media at the same time , such as : user id by A001 Users of , Focus on the media id The data is 1010,1020,1031. In order to facilitate the later analysis of fans' interests , Please split this situation in the table into multiple .
For example, for users A001, Its conversion is as follows :
【 Their thinking 】
Such problems are called “ Column turned ”, stay MySQL There are generally three steps to deal with it :
1) Create a “ Sequence table ”;
2) Join multiple tables , Copy each piece of data in the original table into multiple pieces ;
3) Use substring_index Function to get the final result .
First step : Create sequence table
“ Sequence table ” It means that there is only one field , Stored is a sequence of numbers , such as :
among ,“ Sequence ” The maximum value of is the maximum number of media a user pays attention to in this problem .
select max(length( Follow the media id) - length(replace( Follow the media id,',','')) + 1) as The maximum number of media attention
from Fan attention table ;The return result is :
Then we need new “ Sequence table ” Namely :
The second step : Multi table join
Use multi table join , Can pass “ Sequence table ” take “ Fan attention table ” Each line of becomes multiple lines .
Here are two points to note :
1) To ensure that every piece of data in the original table is not lost , choice “ Left link ”, And take the original table as the left table ;
2) The number of copies is limited in the connection condition , The limiting condition is the number of media users pay attention to , namely “ Follow the media id” The number of commas under the field plus 1.
select t1. user id,
t1. Follow the media id,
t1. date ,
t2. Sequence
from Fan attention table t1
left join Sequence table t2 on t2. Sequence <= (length( Follow the media id) - length(replace( Follow the media id,',','')) + 1);The return result is :
The third step : Use the function to get the result
The next step is to put the media id Intercept it , You need to use the string interception function :SUBSTRING_INDEX.
SUBSTRING_INDEX( character string , Separator , Parameters )among , Separator refers to dividing media in this question id Of “,”;2 Means to separate by separator , Intercept several media from left to right id; If the parameter is negative , It means to intercept several media from right to left id.
select t1. user id,
substring_index(substring_index(t1. Follow the media id,',',t2. Sequence ),',',-1) as Follow the media id,
t1. date
from Fan attention table t1
left join Sequence table t2 on t2. Sequence <= (length( Follow the media id) - length(replace( Follow the media id,',','')) + 1);The return result is :
【 The test point of this question 】
1) Check your understanding of the ordered list ;
2) Check the string interception function SUBSTRING_INDEX Understanding ;
3) Check your understanding of multi table connections .
边栏推荐
- [daily question 1] 735. Planetary collision
- [函数文档] torch.histc 与 paddle.histogram 与 numpy.histogram
- Installing MySQL under Linux
- Angr (XI) - official document (Part2)
- Leetcode 18. sum of four numbers
- Handling of web page image loading errors
- 欧拉路/欧拉回路
- What is the core value of testing?
- [learning record] data enhancement 1
- Keil Chinese garbled code solution
猜你喜欢

Comprehensively analyze the differences between steam and maker Education

Rendering process, how the code becomes a page (I)

Evolution of ape counseling technology: helping teaching and learning conceive future schools

Redis类型

如何在 FastReport VCL 中通过 Outlook 发送和接收报告?

王爽汇编语言详细学习笔记三:寄存器(内存访问)

低代码是开发的未来吗?浅谈低代码平台
![[paper notes] - low illumination image enhancement - zeroshot - rrdnet Network - 2020-icme](/img/e3/f9c6bfdbcd5dffd406e3f1d2331050.png)
[paper notes] - low illumination image enhancement - zeroshot - rrdnet Network - 2020-icme

Simulink automatically generates STM32 code details

Dynamic SQL and paging
随机推荐
[每日一氵]上古年代的 Visual Studio2015 安装
RT_ Use of thread mailbox
Analysis of the reason why easycvr service can't be started and tips for dealing with easy disk space filling
HDU 3585 maximum shortest distance
Redis type
Driving the powerful functions of EVM and xcm, how subwallet enables Boca and moonbeam
FreeRTOS learning (I)
Pipe /createpipe
HDU 1530 maximum clique
Is low code the future of development? On low code platform
Transformer -- Analysis and application of attention model
Rendering process, how the code becomes a page (2)
What is the core value of testing?
欧拉路/欧拉回路
Have you learned the common SQL interview questions on the short video platform?
alter和confirm,prompt的区别
HDU 1522 marriage is stable
How to quickly locate bugs? How to write test cases?
Mysql database -- first knowledge database
Use and expansion of fault tolerance and fusing