Mysql 如何根据引用对类别级别进行分组

Mysql 如何根据引用对类别级别进行分组,mysql,prestashop,Mysql,Prestashop,你好,我被这个查询卡住了(不工作) 我想要以下结果,显示在带有字段的行中: ps_product.reference>>ps_category_lang.name(其中ps_category.nivel=1)作为cat1>>ps_category_lang.name(其中ps_category.nivel=2)作为cat2>>ps_category_lang.name(其中ps_category.nivel=3)作为cat3 您似乎需要某种透视查询。请尝试以下版本: 选择 psc.referen

你好,我被这个查询卡住了(不工作)

我想要以下结果,显示在带有字段的行中: ps_product.reference>>ps_category_lang.name(其中ps_category.nivel=1)作为cat1>>ps_category_lang.name(其中ps_category.nivel=2)作为cat2>>ps_category_lang.name(其中ps_category.nivel=3)作为cat3


您似乎需要某种透视查询。请尝试以下版本:

选择
psc.reference,
最大值(psc.nivel=1时的情况,然后是某些列端)为cat1,
最大值(psc.nivel=2时的情况,然后是一些柱端)为cat2,
最大值(psc.nivel=3时的情况,然后是某些列端)为cat3
来自ps_类别psc
左连接ps\U类别\U lang pscl
关于pscl.id\u类别=psc.id\u类别
左连接ps\U类别\U产品pscp
关于psc.id\u类别=pscp.id\u类别
左连接ps_产品psp
在psp.id_产品上=pscp.id_产品
哪里
psc.nivel IN(1,2,3)和
psc.active=1和
psp.active=1
分组
psc.reference;

请注意,您需要将
某些列
替换为包含类别实际数据的别名/列。您发布的原始问题不包含此信息。

为所有表提供CREATE TABLE、INSERT INTO中的一些示例数据、此数据的所需输出以及精确的MySQL版本信息。可能会跳过不必要的列。@Akina感谢您的输入,我用create and insert编辑了我的问题,感谢您提供的所有帮助。谢谢@Tim Biegeleisen,但对我来说,这不起作用,可能是我的问题错误。。。我有4张桌子要加入。ps_类别(字段为id_类别和nivel)ps_类别语言(字段为id_类别和名称)ps_产品(字段为id_产品和参考)ps_类别产品(字段为id_类别和id_产品),因此对于结果,我需要ps_产品参考,ps_类别语言名称为cat1,ps_产品参考,ps_类别语言名称为cat2,ps_product.reference,ps_category_lang.name as cat3Thanks@Tim Biegeleisen我编辑了这个问题,因为我觉得不清楚。
SELECT 
ps_product.reference,cat1,cat2,cat3
FROM ps_category 
    LEFT JOIN ps_category_lang ON ps_category_lang.id_category = ps_category.id_category 
    LEFT JOIN ps_category_product ON ps_category.id_category = ps_category_product.id_category 
    LEFT JOIN ps_product ON ps_product.id_product = ps_category_product.id_product 
WHERE 
(ps_category.nivel = 1 ) cat1,
(ps_category.nivel = 2 ) cat2,
(ps_category.nivel = 3 ) cat3
and 
and ps_category.active = 1 and ps_product.active = 1
CREATE TABLE `ps_product` (
  `id_product` int(10) UNSIGNED NOT NULL,
  `id_category_default` int(10) UNSIGNED DEFAULT NULL,
  `reference` varchar(32) DEFAULT NULL,
  `active` tinyint(1) UNSIGNED NOT NULL DEFAULT 0
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `ps_product` (`id_product`, `id_category_default`,  `reference`, `active`) VALUES
(1, 30, 'PROD1', 1),
(2, 31, 'PROD2', 1),
(3, 32, 'PROD3', 1),
(4, 33, 'PROD4', 1),
(5, 34, 'PROD5', 1),
(6, 35, 'PROD6', 1),
(7, 36, 'PROD7', 1),
(8, 37, 'PROD8', 1),
(9, 38, 'PROD9', 1),
(10, 39, 'PROD10', 1);

CREATE TABLE `ps_category_lang` (
  `id_category` int(10) UNSIGNED NOT NULL,
  `name` varchar(128) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


INSERT INTO `ps_category_lang` (`id_category`, `name`) VALUES
(1,'Root'),
(10,'MainCat1'),
(11,'MainCat2'),
(12,'MainCat3'),
(13,'MainCat4'),
(14,'MainCat5'),
(15,'MainCat6'),
(16,'ChildMainCat1'),
(17,'ChildMainCat2'),
(18,'ChildMainCat3'),
(19,'ChildMainCat4'),
(20,'ChildMainCat5'),
(21,'ChildMainCat6'),
(22,'ChildMainCat6'),
(23,'ChildMainCat3'),
(24,'ChildMainCat3'),
(25,'ChildMainCat3'),
(26,'ChildMainCat2'),
(27,'ChildMainCat4'),
(28,'ChildMainCat5'),
(29,'ChildMainCat5'),
(30,'OtherChildMainCat1'),
(31,'OtherChildMainCat2'),
(32,'OtherChildMainCat3'),
(33,'OtherChildMainCat4'),
(34,'OtherChildMainCat5'),
(35,'OtherChildMainCat6'),
(36,'OtherChildMainCat6'),
(37,'OtherChildMainCat3'),
(38,'OtherChildMainCat3'),
(39,'OtherChildMainCat3');

CREATE TABLE `ps_category_product` (
  `id_category` int(10) UNSIGNED NOT NULL,
  `id_product` int(10) UNSIGNED NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;



INSERT INTO `ps_category_product` (`id_category`, `id_product`) VALUES
(1,10),
(1,16),
(1,30),
(2,11),
(2,17),
(2,31),
(3,12),
(3,18),
(3,32),
(4,13),
(4,27),
(5,12),
(5,25),
(5,39);

CREATE TABLE `ps_category` (
  `id_category` int(10) UNSIGNED NOT NULL,
  `id_parent` int(10) UNSIGNED NOT NULL,
  `active` tinyint(1) UNSIGNED NOT NULL DEFAULT 0,
  `nivel` int(11) UNSIGNED DEFAULT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=utf8;



INSERT INTO `ps_category` (`id_category`, `id_parent`, `active`, `nivel`) VALUES
(1,0,1,0),
(10,1,1,1),
(11,1,1,1),
(12,1,1,1),
(13,1,1,1),
(14,1,1,1),
(15,1,1,1),
(16,10,1,2),
(17,11,1,2),
(18,12,1,2),
(19,13,1,2),
(20,14,1,2),
(21,15,1,2),
(22,15,1,2),
(23,12,1,2),
(24,12,1,2),
(25,12,1,2),
(26,11,1,2),
(27,13,1,2),
(28,14,1,2),
(29,14,1,2),
(30,16,1,3),
(31,17,1,3),
(32,18,1,3),
(33,19,1,3),
(34,20,1,3),
(35,21,1,3),
(36,22,1,3),
(37,23,1,3),
(38,24,1,3),
(39,25,1,3);