1. <small id='GHrZS'></small><noframes id='GHrZS'>

    2. <i id='GHrZS'><tr id='GHrZS'><dt id='GHrZS'><q id='GHrZS'><span id='GHrZS'><b id='GHrZS'><form id='GHrZS'><ins id='GHrZS'></ins><ul id='GHrZS'></ul><sub id='GHrZS'></sub></form><legend id='GHrZS'></legend><bdo id='GHrZS'><pre id='GHrZS'><center id='GHrZS'></center></pre></bdo></b><th id='GHrZS'></th></span></q></dt></tr></i><div id='GHrZS'><tfoot id='GHrZS'></tfoot><dl id='GHrZS'><fieldset id='GHrZS'></fieldset></dl></div>

      <legend id='GHrZS'><style id='GHrZS'><dir id='GHrZS'><q id='GHrZS'></q></dir></style></legend>
          <bdo id='GHrZS'></bdo><ul id='GHrZS'></ul>
      1. <tfoot id='GHrZS'></tfoot>

        使用 MySQL JSON 字段加入表

        时间:2023-06-26
        1. <tfoot id='4yXAx'></tfoot>
          <i id='4yXAx'><tr id='4yXAx'><dt id='4yXAx'><q id='4yXAx'><span id='4yXAx'><b id='4yXAx'><form id='4yXAx'><ins id='4yXAx'></ins><ul id='4yXAx'></ul><sub id='4yXAx'></sub></form><legend id='4yXAx'></legend><bdo id='4yXAx'><pre id='4yXAx'><center id='4yXAx'></center></pre></bdo></b><th id='4yXAx'></th></span></q></dt></tr></i><div id='4yXAx'><tfoot id='4yXAx'></tfoot><dl id='4yXAx'><fieldset id='4yXAx'></fieldset></dl></div>

            <bdo id='4yXAx'></bdo><ul id='4yXAx'></ul>

              <small id='4yXAx'></small><noframes id='4yXAx'>

            1. <legend id='4yXAx'><style id='4yXAx'><dir id='4yXAx'><q id='4yXAx'></q></dir></style></legend>

                  <tbody id='4yXAx'></tbody>

                • 本文介绍了使用 MySQL JSON 字段加入表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个存储 id 列表的 json 字段(我知道这里不是最佳实践),我想知道是否可以在这个 JSON 字段上使用 do 操作并在 sql 中使用它们.

                  I have a json field that stores a list of ids (not best practice here I know), I want to know if it's possible to use do operations on this JSON field and use them in the sql.

                  下面是我试图实现的虚构示例,这样的事情可行吗?

                  Below is a fictitious example of what I'm trying to achieve, is something like this doable?

                  CREATE TABLE user (
                      user_id INT,
                      user_name VARCHAR(50),
                      user_groups JSON
                  );
                  
                  CREATE TABLE user_group (
                      user_group_id INT,
                      group_name VARCHAR(50)
                  );
                  
                  INSERT INTO user_group (user_group_id, group_name) VALUES (1, 'Group A');
                  INSERT INTO user_group (user_group_id, group_name) VALUES (2, 'Group B');
                  INSERT INTO user_group (user_group_id, group_name) VALUES (3, 'Group C');
                  
                  INSERT INTO user (user_id, user_name, user_groups) VALUES (101, 'John', '[1,3]');
                  

                  根据上述数据,我想设计一个查询,得到如下结果:

                  With the above data I would like to fashion a query that gives me the results like this:

                  user_id | user_name | user_group_id | group_name|
                  -------------------------------------------------
                  101     | John      | 1             | Group A
                  101     | John      | 3             | Group C
                  

                  我正在考虑的一些伪样式 SQL 如下,但我仍然不知道这是否可行,或者 mysql 提供了哪些 JSON 函数来实现这一点

                  Some psuedo style SQL I'm thinking is below, though I still have no clue if this is possible, or what JSON functions mysql offers I would use to achieve this

                   SELECT 
                         u.user_id, 
                         u.user_name, 
                         g.user_group_id
                         g.group_name
                     FROM users u
                     LEFT JOIN user_group g on g.user_group_id in some_json_function?(u.user_groups)
                  

                  如果问题不清楚,请告诉我.

                  Let me know if the question isn't clear.

                  推荐答案

                  借助 Feras 的评论和一些摆弄:

                  With the help of Feras's comment and some fiddling:

                    SELECT 
                         u.user_id, 
                         u.user_name, 
                         g.user_group_id,
                         g.group_name
                     FROM user u
                     LEFT JOIN user_group g on JSON_CONTAINS(u.user_groups, CAST(g.user_group_id as JSON), '$')
                  

                  这似乎有效,请告诉我是否有更好的方法.

                  This appears to work, let me know if there's a better way.

                  这篇关于使用 MySQL JSON 字段加入表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 SQL 中对包含数字的字符串列进行排序? 下一篇:mySQL 查询在数据库中的所有表中搜索字符串?

                  相关文章

                      <bdo id='WLraa'></bdo><ul id='WLraa'></ul>
                  1. <i id='WLraa'><tr id='WLraa'><dt id='WLraa'><q id='WLraa'><span id='WLraa'><b id='WLraa'><form id='WLraa'><ins id='WLraa'></ins><ul id='WLraa'></ul><sub id='WLraa'></sub></form><legend id='WLraa'></legend><bdo id='WLraa'><pre id='WLraa'><center id='WLraa'></center></pre></bdo></b><th id='WLraa'></th></span></q></dt></tr></i><div id='WLraa'><tfoot id='WLraa'></tfoot><dl id='WLraa'><fieldset id='WLraa'></fieldset></dl></div>
                    <legend id='WLraa'><style id='WLraa'><dir id='WLraa'><q id='WLraa'></q></dir></style></legend>

                    <tfoot id='WLraa'></tfoot>

                    <small id='WLraa'></small><noframes id='WLraa'>