友人推薦(列転行、help_topic_id)


文書ディレクトリ
  • 前言
  • 一、テーマ:友人推薦
  • 二、コードブロック
  • 1.ソースコード
  • 2.SQL分解

  • 前言
    この問題は主にmysqlを結合することを分かち合います.help_topicというシステム表は列転行のいくつかの巧みな解題思想をしている.
    一、テーマ:友達の推薦
    1、2人は友达ではありませんが、共通の友达がいて、互いに推荐することができます(共通の友达はn个で、推荐指数はn)2、例えば:明ちゃんと王さんは友达ではありませんが、共通の友达の孙がいます.そのため、明ちゃんを王さんに推荐することができます.王さんを明ちゃんに推荐することもできます.
    create table user_friends(
    	user varchar(5),
    	friends text
    );
    
    insert into user_friends(user,friends)values
    ('  ','  ,  ,  '),
    ('  ','  ,  '),
    ('  ','  ,  ,  '),
    ('  ','  ,  ,  '),
    ('  ','  ');
    

    4、需要結果展示:
    --   
    +------+----------------+
    | user | friends        |
    +------+----------------+
    |    |   ,  ,   |
    |    |   ,        |
    |    |   ,  ,   |
    |    |   ,  ,   |
    |    |              |
    +------+----------------+
    --     
    +-------------+----------+
    |         |      |
    +-------------+----------+
    |   <->   | 2        |
    |   <->   | 1        |
    |   <->   | 1        |
    +-------------+----------+
    

    二、コードブロック
    1.ソースコード
    コードは以下の通りです(まずsqlを貼って、みんなは自分で考えて、後で思想を分解します):
    -- 1、           
    select view V_friends 
    AS
    	select
    		user,substring_index(substring_index(U.friends,',',M.help_topic_id+1),',',-1)friend
    	from
    		user_friends U,mysql.help_topic M
    	where
    		M.help_topic_id<=length(U.friends)-length(replace(U.friends,',',''))
    
    -- 2、
    select
    	concat(if(A.user<B.user,A.user,B.user),'',if(A.user>B.user,A.user,B.user))    ,
    	round(count(1)/2) as     
    from
    	V_friends A
    cross join
    	V_friends B
    on
    	A.user!=B.user and
    	A.friend=B.friend and
    	A.user not in (select friend from V_friends where user=B.user)
    group by
    	    
    order by
    	     DESC;
    

    2.SQL分解
    コードは次のとおりです(例).
    -- user_friends    mysql.help_topic      
    -- where      mysql.help_topic    
    	--       ???
    select 
    	user,friends,help_topic_id 
    from 
    	user_friends,mysql.help_topic 
    where 
    	help_topic_id<5 
    order by 
    	user,friends;
    
    --   user_friends    friends           mysql.help_topic   
    select
    	user,help_topic_id
    from
    	user_friends U,mysql.help_topic M
    where
    	M.help_topic_id<=length(U.friends)-length(replace(U.friends,',',''))
    
    
    --           friends       help_topic_id       help_topic_id      
    select view V_friends 
    AS
    	select
    		user,substring_index(substring_index(U.friends,',',M.help_topic_id+1),',',-1)friend
    	from
    		user_friends U,mysql.help_topic M
    	where
    		M.help_topic_id<=length(U.friends)-length(replace(U.friends,',',''))
    	
    --      	
    select
    	concat(if(A.user<B.user,A.user,B.user),'',if(A.user>B.user,A.user,B.user))    ,
    	round(count(1)/2) as     
    from
    	V_friends A
    cross join
    	V_friends B
    on
    	A.user!=B.user and
    	A.friend=B.friend and
    	A.user not in (select friend from V_friends where user=B.user)
    group by
    	    
    order by
    	     DESC;