获取产品及其变体
Get products and their variants
我需要帮助解决一个问题。我需要一个从表中获取产品或变体的查询。所以,如果我需要产品“a”,“b”+ 产品变体“a1”。 looks将如何查询?
我需要从表中获取三种产品和最后一种产品(ID:a)并替换为修改后的值(价格、重量、销售额)
这是表和行的示例:
products
------------------
CREATE TABLE IF NOT EXISTS `products` (
`id` varchar(10) NOT NULL,
`hidden` tinyint(1) NOT NULL,
`only_variations` int(1) NOT NULL DEFAULT '0',
`price` decimal(20,6) NOT NULL,
`weight_in_package` int(11) NOT NULL COMMENT 'mg',
`stock_pieces` int(11) NOT NULL,
`gifts` text NOT NULL,
`rating` int(11) NOT NULL,
`sold_pieces` int(11) NOT NULL,
`brand` int(11) NOT NULL,
`deleted` int(1) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `products` (`id`, `hidden`, `only_variations`, `price`, `weight_in_package`, `stock_pieces`, `gifts`, `rating`, `sold_pieces`, `brand`, `deleted`) VALUES
('a', 0, 0, 12.000000, 50, 10, '', 0, 35, 1, 0),
('b', 0, 0, 11.000000, 50, 15, '', 0, 22, 2, 0);
variations
------
CREATE TABLE IF NOT EXISTS `product_variations` (
`id` varchar(10) COLLATE utf8_bin NOT NULL,
`product` varchar(10) COLLATE utf8_bin NOT NULL,
`price` decimal(20,6) NOT NULL,
`stock_pieces` int(11) NOT NULL,
`weight` int(11) NOT NULL COMMENT 'mg',
`sales` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
INSERT INTO `product_variations` (`id`, `product`, `price`, `stock_pieces`, `weight`, `sales`) VALUES
('a1', 'a', 50.000000, 10, 0, 0),
('a2', 'a', 40.000000, 11, 0, 0);
我在 sql 查询中需要这个输出:
id | price | variation
a 12 NULL
b 11 NULL
a 50 a1
非常感谢您的帮助!
试试这个:http://sqlfiddle.com/#!9/0f128/44/0
SELECT
*,
null as `variation`
FROM `products` WHERE id IN('a', 'b')
UNION ALL
(SELECT
p.id as `id`,
p.hidden as `hidden`,
p.only_variations as `only_variations`,
v.price as `price`,
p.weight_in_package as `weight_in_package`,
v.stock_pieces as `stock_pieces`,
p.gifts as `gifts`,
p.rating as `rating`,
p.sold_pieces as `sold_pieces`,
p.brand as `brand`,
p.deleted as `deleted`,
v.id as `variation`
FROM `product_variations` as v
LEFT JOIN `products` as p ON p.id = v.product
WHERE v.id IN('a1'))
ORDER BY price DESC
我需要帮助解决一个问题。我需要一个从表中获取产品或变体的查询。所以,如果我需要产品“a”,“b”+ 产品变体“a1”。 looks将如何查询? 我需要从表中获取三种产品和最后一种产品(ID:a)并替换为修改后的值(价格、重量、销售额)
这是表和行的示例:
products
------------------
CREATE TABLE IF NOT EXISTS `products` (
`id` varchar(10) NOT NULL,
`hidden` tinyint(1) NOT NULL,
`only_variations` int(1) NOT NULL DEFAULT '0',
`price` decimal(20,6) NOT NULL,
`weight_in_package` int(11) NOT NULL COMMENT 'mg',
`stock_pieces` int(11) NOT NULL,
`gifts` text NOT NULL,
`rating` int(11) NOT NULL,
`sold_pieces` int(11) NOT NULL,
`brand` int(11) NOT NULL,
`deleted` int(1) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `products` (`id`, `hidden`, `only_variations`, `price`, `weight_in_package`, `stock_pieces`, `gifts`, `rating`, `sold_pieces`, `brand`, `deleted`) VALUES
('a', 0, 0, 12.000000, 50, 10, '', 0, 35, 1, 0),
('b', 0, 0, 11.000000, 50, 15, '', 0, 22, 2, 0);
variations
------
CREATE TABLE IF NOT EXISTS `product_variations` (
`id` varchar(10) COLLATE utf8_bin NOT NULL,
`product` varchar(10) COLLATE utf8_bin NOT NULL,
`price` decimal(20,6) NOT NULL,
`stock_pieces` int(11) NOT NULL,
`weight` int(11) NOT NULL COMMENT 'mg',
`sales` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
INSERT INTO `product_variations` (`id`, `product`, `price`, `stock_pieces`, `weight`, `sales`) VALUES
('a1', 'a', 50.000000, 10, 0, 0),
('a2', 'a', 40.000000, 11, 0, 0);
我在 sql 查询中需要这个输出:
id | price | variation
a 12 NULL
b 11 NULL
a 50 a1
非常感谢您的帮助!
试试这个:http://sqlfiddle.com/#!9/0f128/44/0
SELECT
*,
null as `variation`
FROM `products` WHERE id IN('a', 'b')
UNION ALL
(SELECT
p.id as `id`,
p.hidden as `hidden`,
p.only_variations as `only_variations`,
v.price as `price`,
p.weight_in_package as `weight_in_package`,
v.stock_pieces as `stock_pieces`,
p.gifts as `gifts`,
p.rating as `rating`,
p.sold_pieces as `sold_pieces`,
p.brand as `brand`,
p.deleted as `deleted`,
v.id as `variation`
FROM `product_variations` as v
LEFT JOIN `products` as p ON p.id = v.product
WHERE v.id IN('a1'))
ORDER BY price DESC