gpt4 book ai didi

mysql - GROUP BY 已与两个子查询分组的另一个表

转载 作者:行者123 更新时间:2023-12-03 17:40:07 24 4
gpt4 key购买 nike

我有这样的 table

Table1

ID | Val | Val2 |
606541 |3175031503131004|3175032612900004|
606542 |3175031503131004|3175032612900004|
677315 |3175031503131004|3175032612980004|
222222 |1111111111111111|8888888888888888|
231233 |1111111111111111|3175032612900004|
111111 |9999992222211111|1111111111111111|
57 |3173012102121018|3173015101870020|
59 |3173012102121018|3173021107460002|
2 |900 |7000 |
4 |900 |7001 |
我有两个条件列 ValVal2 .如果 Val 显示结果:
  • Val 列至少有两个或多个重复值
  • Val2 列没有重复值(唯一)

  • 例如 :
    示例 1
     ID      |    Val         |     Val2       |
    606541 |3175031503131004|3175032612900004|
    606542 |3175031503131004|3175032612900004|
    677315 |3175031503131004|3175032612980004|

    False, because even the Val column
    had two or more duplicate but the Val2
    had dulicate value (ID 606541 and 606542)
    样本预期 1 结果
     No records
    样本 2
     ID      |    Val         |     Val2       |
    222222 |1111111111111111|8888888888888888|
    231233 |1111111111111111|3175032612900004|
    111111 |9999992222211111|1111111111111111|

    True, Because the condition is match,
    Val column had duplicate value AND Val2 had unique values
    样本 2 预期结果
     ID      |    Val         |     Val2       |
    222222 |1111111111111111|8888888888888888|
    231233 |1111111111111111|3175032612900004|
    示例 3
     ID      |    Val         |     Val2       |
    606541 |3175031503131004|3175032612900004|
    606542 |3175031503131004|3175032612900004|
    677315 |3175031503131004|3175032612980004|
    222222 |1111111111111111|8888888888888888|
    231233 |1111111111111111|3175032612900004|
    111111 |9999992222211111|1111111111111111|

    Note : This is false condition, Because even the value for id 606541, 606542, and
    677315 in column Val had duplicate value at least
    two or more but the value in column Val2 had no unique value (it could be true condition if id 606541,
    606542, and 677315 had 3 different value on Val2).

    NOte 2 : for Id 222222 and 231233 that had duplicate value, this is still false, because the column
    Val2 with ID 231233 had the same value with ID 606542 and 606541 (3175032612900004), so it didnt match
    the second condition which only have no duplicate value
    样本 3 预期结果
     No records
    现在回到 Table1在早些时候,我试图用这个查询显示两个条件的结果
    SELECT
    tb.* FROM table1 tb
    WHERE
    tb.Val2 IN (
    SELECT ta.Val2
    FROM (
    SELECT
    t.*
    FROM
    table1 t
    WHERE
    t.Val IN (
    SELECT Val FROM table1
    GROUP BY Val
    HAVING count( Val ) > 1 )
    ) ta
    GROUP BY
    ta.Val2
    HAVING
    count( ta.Val2 ) = 1
    )
    结果
    ID         Val                   Val2
    677315 3175031503131004 3175032612980004
    222222 1111111111111111 8888888888888888
    57 3173012102121018 3173015101870020
    59 3173012102121018 3173021107460002
    2 900 7000
    4 900 7001
    虽然我希望结果是这样的:
    ID         Val                   Val2
    57 3173012102121018 3173015101870020
    59 3173012102121018 3173021107460002
    2 900 7000
    4 900 7001
    我的查询有问题吗?
    这是我的 DB Fiddle .

    最佳答案

    请原谅任何错误,因为这将是我在本论坛的第一个答案。
    你也可以试试下面的,我同意窗口函数的答案。

    SELECT t.*
    FROM table1 t
    WHERE t.val IN (SELECT val
    FROM table1
    GROUP BY val
    HAVING COUNT(val) > 1
    AND COUNT(val) = COUNT(DISTINCT val2)
    )
    AND t.val NOT IN (SELECT t.val
    FROM table1 t
    WHERE EXISTS (SELECT 1
    FROM table1 tai
    WHERE tai.id != t.id
    AND tai.val2 = t.val2));
    /*
    where 子句的第一部分确保我们在列 val2 中有不同的值,用于列 val 中的重复值
    带有 not in 的 where 子句的第二部分告诉我们,对于列 val2 中的值,不同 id 之间没有值共享
    */
    --逆序查询(不确定给出预期结果)
    SELECT t.*
    FROM table2 t
    WHERE t.val IN (SELECT val FROM table2 GROUP BY val HAVING COUNT(val) = 1)
    AND t.val2 IN (SELECT t.val2
    FROM table2 ta
    WHERE EXISTS (SELECT 1
    FROM table2 tai
    WHERE tai.id != ta.id
    AND tai.val = ta.val));

    关于mysql - GROUP BY 已与两个子查询分组的另一个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62765510/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com