0

我正在使用find_in_set以下查询来获取数据,它可以正常工作。

SELECT * 
FROM A 
WHERE FIND_IN_SET(
column1,
(
 SELECT column1 
 FROM B 
 WHERE  id = 21)
);

在这里,这个查询SELECT column1 FROM B WHERE id = 21给出了类似的结果,'175587,282329' 但我希望'175587,282329'我应该使用的最高值会出现在这里。在这种情况下,它将是282329.but 将是任意数量的逗号分隔值。谢谢

4

2 回答 2

1

虽然我并不认真提倡将此作为解决方案,但请考虑以下黑客...

 DROP TABLE IF EXISTS my_table;

 CREATE TABLE my_table
 (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
 ,string VARCHAR(20) NOT NULL UNIQUE
 );

 INSERT INTO my_table(string) VALUES
 ('1,2,3'),
 ('2,3,4'),
 ('3,4,5'),
 ('1,3,5'),
 ('2,4,6');

 SELECT * FROM my_table;
 +----+--------+
 | id | string |
 +----+--------+
 |  1 | 1,2,3  |
 |  4 | 1,3,5  |
 |  2 | 2,3,4  |
 |  5 | 2,4,6  |
 |  3 | 3,4,5  |
 +----+--------+

 SELECT * FROM ints;
 +---+
 | i |
 +---+
 | 0 |
 | 1 |
 | 2 |
 | 3 |
 | 4 |
 | 5 |
 | 6 |
 | 7 |
 | 8 |
 | 9 |
 +---+

 SELECT x.*
      , GROUP_CONCAT(DISTINCT SUBSTRING_INDEX(SUBSTRING_INDEX(string,',',i+1),',',-1) ORDER BY SUBSTRING_INDEX(SUBSTRING_INDEX(string,',',i),',',-1) DESC) n 
   FROM my_table x
      , ints
  GROUP 
     BY id;

 +----+--------+-------+
 | id | string | n     |
 +----+--------+-------+
 |  1 | 1,2,3  | 3,2,1 |
 |  2 | 2,3,4  | 4,3,2 |
 |  3 | 3,4,5  | 5,4,3 |
 |  4 | 1,3,5  | 5,3,1 |
 |  5 | 2,4,6  | 6,4,2 |
 +----+--------+-------+
于 2014-08-09T11:03:13.600 回答
1

假设 TableA 看起来像那样

CREATE TABLE A (
    id INT,
    columnA INT
);

以下方法将为您提供最多 100 个分隔值的所需结果(据我所知):

SELECT * FROM A
INNER JOIN (
    SELECT MAX(t.value) as max_value
    FROM (
        SELECT
            id, 
            SUBSTRING_INDEX(SUBSTRING_INDEX(column1, ',', n.n), ',', -1) value
        FROM B CROSS JOIN (
            -- build for up to 100 separated values
            SELECT 
                a.N + b.N * 10 + 1 AS n
            FROM
                (SELECT 0 AS N UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) a
               ,(SELECT 0 AS N UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) b
            ORDER BY n
        ) n
        WHERE n <= (1 + LENGTH(column1) - LENGTH(REPLACE(column1, ',', '')))
        AND B.id = 21
    ) t
) t1
ON A.columnA = t1.max_value
;

演示

解释

最内部的 SELECT 创建一个临时表,其值为 1 到 100:

    SELECT 
        a.N + b.N * 10 + 1 AS n
    FROM
        (SELECT 0 AS N UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) a
       ,(SELECT 0 AS N UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) b
    ORDER BY n

此方法是生成此类列表的常用方法,并且速度很快。

嵌套的 SUBSTRING 调用负责获取我们的值,用

SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(column1, ',', 1), ',', -1) FROM B
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(column1, ',', 2), ',', -1) FROM B

感受一下它的作用。我们限制我们的搜索

1 + LENGTH(column1) - LENGTH(REPLACE(column1, ',', ''))

因为我们的值比值少一个逗号,我们也需要最后一个值。所以声明

SELECT
    id, 
    SUBSTRING_INDEX(SUBSTRING_INDEX(column1, ',', n.n), ',', -1) value
FROM B CROSS JOIN (
    SELECT 
        a.N + b.N * 10 + 1 AS n
    FROM
        (SELECT 0 AS N UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) a
       ,(SELECT 0 AS N UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) b
    ORDER BY n
) n
WHERE n <= (1 + LENGTH(column1) - LENGTH(REPLACE(column1, ',', '')))
AND B.id = 21

将返回对应于 id = 21 的 column1 的值列表。

剩下的就是将此列表的最大值简单连接到 TableA 的相应列。

于 2014-08-09T11:41:58.863 回答