1
我需要幫助請一個查詢。我需要一個能夠從表格中獲取產品或變體的查詢。所以,如果我需要的產品 「一個」,產品 「A1」 的 「b」 + 變化。外觀查詢將如何? 我需要得到從表和到最後產品的三種產品(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
非常感謝您的幫助!
'選擇\'ID \'\'價格\',null作爲\'變化\'由\'產品\'UNION SELECT \'產品\' '\',\'價格\',\'id \'作爲變體\'從\'product_variations \'WHERE \'id \'='a1''? - http://sqlfiddle.com /#!9/0f128/2 – Sean