<bdo id='u7urD'></bdo><ul id='u7urD'></ul>

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

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

    2. FIND_IN_SET() 与 IN()

      时间:2023-08-19

      <legend id='6Vrw6'><style id='6Vrw6'><dir id='6Vrw6'><q id='6Vrw6'></q></dir></style></legend>
      <tfoot id='6Vrw6'></tfoot>

    3. <i id='6Vrw6'><tr id='6Vrw6'><dt id='6Vrw6'><q id='6Vrw6'><span id='6Vrw6'><b id='6Vrw6'><form id='6Vrw6'><ins id='6Vrw6'></ins><ul id='6Vrw6'></ul><sub id='6Vrw6'></sub></form><legend id='6Vrw6'></legend><bdo id='6Vrw6'><pre id='6Vrw6'><center id='6Vrw6'></center></pre></bdo></b><th id='6Vrw6'></th></span></q></dt></tr></i><div id='6Vrw6'><tfoot id='6Vrw6'></tfoot><dl id='6Vrw6'><fieldset id='6Vrw6'></fieldset></dl></div>
      • <small id='6Vrw6'></small><noframes id='6Vrw6'>

            <tbody id='6Vrw6'></tbody>
              • <bdo id='6Vrw6'></bdo><ul id='6Vrw6'></ul>
                本文介绍了FIND_IN_SET() 与 IN()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我的数据库中有 2 个表.一种用于订单,一种用于公司.

                I have 2 tables in my database. One is for orders, and one is for companies.

                订单具有以下结构:

                OrderID     |     attachedCompanyIDs
                ------------------------------------
                   1                     1,2,3
                   2                     2,4
                

                公司有这样的结构:

                CompanyID      |        name
                --------------------------------------
                    1                 Company 1
                    2                 Another Company
                    3                 StackOverflow
                    4                 Nothing
                

                要获取订单的公司名称,我可以执行以下查询:

                To get an order's companies names, I can do a query as such:

                SELECT name FROM orders,company
                WHERE orderID = 1 AND FIND_IN_SET(companyID, attachedCompanyIDs)
                

                该查询工作正常,但以下查询无效.

                That query works fine, but the following query does not.

                SELECT name FROM orders,company
                WHERE orderID = 1 AND companyID IN (attachedCompanyIDs)
                

                为什么第一个查询有效,而第二个无效?

                Why does the first query work but not the second one?

                第一个查询返回:

                name
                ---------------
                Company 1
                Another Company
                StackOverflow
                

                第二个查询只返回:

                name
                ---------------
                Company 1
                

                这是为什么,为什么第一个查询返回所有公司,而第二个查询只返回第一个?

                Why is this, why does the first query return all the companies, but the second query only returns the first one?

                推荐答案

                SELECT  name
                FROM    orders,company
                WHERE   orderID = 1
                        AND companyID IN (attachedCompanyIDs)
                

                attachedCompanyIDs 是一个标量值,它被转换为 INT(companyID 的类型).

                attachedCompanyIDs is a scalar value which is cast into INT (type of companyID).

                演员表只返回直到第一个非数字的数字(在你的情况下是逗号).

                The cast only returns numbers up to the first non-digit (a comma in your case).

                因此,

                companyID IN ('1,2,3') ≡ companyID IN (CAST('1,2,3' AS INT)) ≡ companyID IN (1)
                

                PostgreSQL中,您可以将字符串转换为数组(或首先将其存储为数组):

                In PostgreSQL, you could cast the string into array (or store it as an array in the first place):

                SELECT  name
                FROM    orders
                JOIN    company
                ON      companyID = ANY (('{' | attachedCompanyIDs | '}')::INT[])
                WHERE   orderID = 1
                

                这甚至会使用 companyID 上的索引.

                and this would even use an index on companyID.

                不幸的是,这在 MySQL 中不起作用,因为后者不支持数组.

                Unfortunately, this does not work in MySQL since the latter does not support arrays.

                您可能会发现这篇文章很有趣(请参阅#2):

                You may find this article interesting (see #2):

                • MySQL 中的 10 件事(不会按预期工作)

                更新:

                如果逗号分隔列表中的值数量有一些合理的限制(比如不超过5),那么你可以尝试使用这个查询:

                If there is some reasonable limit on the number of values in the comma separated lists (say, no more than 5), so you can try to use this query:

                SELECT  name
                FROM    orders
                CROSS JOIN
                        (
                        SELECT  1 AS pos
                        UNION ALL
                        SELECT  2 AS pos
                        UNION ALL
                        SELECT  3 AS pos
                        UNION ALL
                        SELECT  4 AS pos
                        UNION ALL
                        SELECT  5 AS pos
                        ) q
                JOIN    company
                ON      companyID = CAST(NULLIF(SUBSTRING_INDEX(attachedCompanyIDs, ',', -pos), SUBSTRING_INDEX(attachedCompanyIDs, ',', 1 - pos)) AS UNSIGNED)
                

                这篇关于FIND_IN_SET() 与 IN()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:哪里 vs 有 下一篇:计算字符串在 VARCHAR 字段中出现的次数?

                相关文章

                最新文章

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

                <small id='1jzTc'></small><noframes id='1jzTc'>

                  1. <tfoot id='1jzTc'></tfoot>

                    • <bdo id='1jzTc'></bdo><ul id='1jzTc'></ul>