从一个表获取记录,其中id在mysql json的第二个表数组中

时间:2019-07-04 11:01:46

标签: mysql sql json database

我的MySQL数据库中有两个具有以下结构的表。

table roles
+----+-----------+---------------------+
| id | name      | description         |
+----+-----------+---------------------+
|  1 | Owner     | Owner of a workspace|
|  2 | Biller    | Biller of workspace |
|  3 | Member    | Member of Workspace |
+----+-----------+---------------------+


table user which contain an array of roles id in JSON data type.
+----+-----------+---------------------+
| id | name      | roles               |
+----+-----------+---------------------+
|  1 | xyz       | [1,2]               |
+----+-----------+---------------------+

现在,表用户包含一个角色ID数组,我想根据条件加入两个表,其中role.id在user.roles中。例如,我想获取一些这样的数据:

从角色中选择*在user.roles中的role.id上将用户加入用户,其中user.id = 1;

+----+-----------+---------------------+
| id | name      | description         |
+----+-----------+---------------------+
|  1 | Owner     | Owner of a workspace|
|  2 | Biller    | Biller of workspace |
+----+-----------+---------------------+

1 个答案:

答案 0 :(得分:0)

效果更好

select * from roles where FIND_IN_SET(id,(select Replace(Replace(roles,']',''),'[','') from user));