如何从MySQL表中获取所有数据组合? [英] How do I get all combinations of data from a MySQL table?

查看:120
本文介绍了如何从MySQL表中获取所有数据组合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了最后五个小时尝试从表格中获取每个产品选项组合,但现在我完全陷入了困境.我有一张表,上面有(简化的)数据:

I have spent the last five hours trying to get every product option combination from a table but I'm totally stuck now. I have a table with data like (simplified):

CREATE TABLE `assigned_options` (
  `option_id` int(10) unsigned NOT NULL DEFAULT '0',
  `value_id` int(10) unsigned NOT NULL DEFAULT '0',
);

INSERT INTO `assigned_options` (`value_id`, `option_id`) VALUES
(4, 2),
(3, 2),
(2, 1),
(1, 1),
(5, 3),
(6, 3),
(7, 3);

说选项ID 2是带有红色(4)和蓝色(3)的颜色,选项id 1的大小等等.

Say option ID 2 is color with variations red (4) and blue (3), option id 1 size etc....

是否可以通过一个MySQL查询来做到这一点?我曾尝试使用PHP制作一个重复出现的函数以获取所有可能性,但我只是无法使其正常工作.

Is it possible to do this with one MySQL query? I have tried using PHP to make a recurring function to get every possibility but I just can't get it to work.

任何提示都非常感谢. :)

Any tips GREATLY appreciated. :)

推荐答案

select option_id, value_id
from assigned_options
group by option_id, value_id
order by option_id, value_id

这篇关于如何从MySQL表中获取所有数据组合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆