MySql确定其他字符串列表中存在的字符串列表的所有元素

时间:2017-10-25 12:32:40

标签: mysql stored-procedures

我有这些表格:

category_table
+--------+-----------------+---------+
| id     | title         | parent_id |
+--------+---------------+-----------+
| 1      | f             | null      |
| 2      | h             | null      |
| 3      | j             | 1         |
| 4      | y             | 3         |
| 5      | d             | 3         |
+--------+-----------------+---------+

post_table
+--------+---------------+-------------+
| id     | title         | categoryies |
+--------+---------------+-------------+
| 1      | title1        | 1           |
| 2      | title2        | 1,2         |
| 3      | title3        | 4,5         |
| 4      | title4        | 4           |
| 5      | title5        | 3           |
+--------+---------------+-------------+

以及查找包含'3,5,2'个类别的所有帖子的过程。

过程如何在其他字符串列表中搜索字符串列表,类似于:

Create Procedure post_select(IN category_ids Varchar(200))
Begin
   Select *
   From post
   Where 
   // all category_ids exists in post categories column
   // such as FIND_IN_SET('1', '1,2,3') but first parameter
   // should be list string for example:
   // FIND_IN_SET('2,3,4','2,4,5,6,7,8')

End;

1 个答案:

答案 0 :(得分:1)

您应该考虑创建一个post_category表,将post_id与category_id一起存储。