mySQL逗号分开了

时间:2012-10-01 06:28:12

标签: mysql comma

我有两张桌子:

表1:用户

| user_Id  | likes       |               
| 100      | 200,201,217 |
| 101      | 200,215,216 |

表2:文章

| article_id| author_id | title             |
| 216       | 100       | This is test      |
| 200       | 101       | this is only test |

当用户成功登录$userId=user_Id时,显示该用户喜欢的所有文章?

我无法弄清楚如何使用逗号从用户表中的likes列中删除记录?

5 个答案:

答案 0 :(得分:1)

您也可以使用FIND_IN_SET

SELECT a.user_id, b.*
FROM user a, articles b
WHERE  FIND_IN_SET(b.article_id, a.likes)

SQLFiddle Demo

答案 1 :(得分:1)

SELECT 
    * 
FROM user 
LEFT JOIN articles on FIND_IN_SET(articles.article_id , likes) 

答案 2 :(得分:0)

select * 
  from articles a,
       user u
 where FIND_IN_SET (a.article_id, likes)

答案 3 :(得分:0)

您可以使用与FIND_IN_SET不同的过程来执行此操作,这样您就可以从索引中获得所有好处,这将使您的数据库增长更快,并且可以更好地扩展:

DELIMITER |
create procedure articles_liked_by_user(user_id_in INT)
BEGIN
set @sql = CONCAT('select * from articles where article_id in (', (select likes from user where user_id = user_id_in), ')');
PREPARE stm from @sql;
EXECUTE stm;
END;
|
DELIMITER ;

使用:

call articles_liked_by_user(100);

答案 4 :(得分:-2)

检查。

 select * 
from articles
where FIND_IN_SET(articles.article_id,(select likes from user where user_id=100));