当前位置:网站首页>How to analyze fans' interests?

How to analyze fans' interests?

2022-07-07 03:00:00 Monkey data analysis

10a18ad8150fd46fa97b3d3b17a431aa.jpeg

【 subject 】

There is a “ Fan attention table ”, contain 3 A field : user id、 Follow the media id、 date .

6e8640f4cb6f99fc4716ca74d20d3a1c.png

【 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 :

621a8a8d541d3167117357f9c4afa031.png

【 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 : establish Sequence table

“ Sequence table ” It means that there is only one field , Stored is a sequence of numbers , such as :

f15d4249f30b0b40bf5eb8f027801636.png

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 :

34da2976eb21223b75d638b1dfb4c937.png

Then we need new “ Sequence table ” Namely :

6614cc56551e6296dd6563fea4421867.png

The second step : Multi table join

8a73c3b183908ccfb71253d299b6fe41.png

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 :

c3fe3d01ee740121ac4e3e25ccf2b2d5.png

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 )

aec578dcb8fe5dc08b65982a90a65edb.png

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 :

1eb1e5531526b4ce30d5a5723cae7fe2.png

【 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 .

ee834fd7274e1d3688365b11ad863dc0.jpeg

4df7baad725fffcf2e47fc88458d4c74.jpeg

▼  Click on 「 Read the original 」

▼  Unlock more data analysis courses

原网站

版权声明
本文为[Monkey data analysis]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207061927308608.html