gpt4 book ai didi

php - 获取 MySQL 中 3 个类别的所有可能组合

转载 作者:行者123 更新时间:2023-11-29 07:07:33 25 4
gpt4 key购买 nike

我有 3 个类别(cate1、cate2、cate3),数据库中有 14 行。我喜欢获得所有可能的组合。

数据库:

id   category   
1 cate1
2 cate2
3 cate3
6 cate1
7 cate2
8 cate3
9 cate1
10 cate2
11 cate3
12 cate1
13 cate2
14 cate3

结果:

[0]
cate1 = 1 (id)
cate2 = 2 (id)
cate3 = 3 (id)
[1]
cate1 = 6 (id)
cate2 = 7 (id)
cate3 = 8 (id)
[2]
cate1 = 9 (id)
cate2 = 10 (id)
cate3 = 11 (id)

..依此类推列出所有组合。

请帮忙

最佳答案

如果我理解正确的话,您需要为每个类别进行交叉连接:

select c1.id, c2.id, c3.id
from (select t.* from t where category = 'cate1') c1 cross join
(select t.* from t where category = 'cate2') c2 cross join
(select t.* from t where category = 'cate3') c3;

关于php - 获取 MySQL 中 3 个类别的所有可能组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41075849/

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