2017-07-27 52 views
0

我做下面的SELECT在不同的列基於價值的MySQL選擇增量指標

SELECT products.supplier_catalog_number, prices.price, 0 AS price_index 
FROM products 
    INNER JOIN prices ON prices.product_id = products.id 
LIMIT 10 

,結果看起來像

+-------------------------+----------+-------------+ 
| supplier_catalog_number | price | price_index | 
+-------------------------+----------+-------------+ 
| 00000001600L   | 287.0000 |   0 | 
| 00000001600L   | 243.9500 |   0 | 
| 0023-200    | 710.0000 |   0 | 
| 0023-200    | 603.5000 |   0 | 
| 0023-300    | 232.0000 |   0 | 
| 0023-300    | 185.6000 |   0 | 
| 0023-301    | 289.0000 |   0 | 
| 0023-301    | 231.2000 |   0 | 
| 0023-301    | 109.0000 |   0 | 
| 0023-301    | 92.6500 |   0 | 
+-------------------------+----------+-------------+ 

我需要的結果是

+-------------------------+----------+-------------+ 
| supplier_catalog_number | price | price_index | 
+-------------------------+----------+-------------+ 
| 00000001600L   | 287.0000 |   0 | 
| 00000001600L   | 243.9500 |   1 | 
| 0023-200    | 710.0000 |   0 | 
| 0023-200    | 603.5000 |   1 | 
| 0023-300    | 232.0000 |   0 | 
| 0023-300    | 185.6000 |   1 | 
| 0023-301    | 289.0000 |   0 | 
| 0023-301    | 231.2000 |   1 | 
| 0023-301    | 109.0000 |   2 | 
| 0023-301    | 92.6500 |   3 | 
+-------------------------+----------+-------------+ 

可以在單個查詢中完成嗎?

+0

「0 AS price_index」您想要在price_index列中顯示什麼?他們從第二張桌子出現在哪裏? – HoTTab1CH

+0

@ HoTTab1CH它們是每個'product.supplier_catalog_number'自動遞增索引' – Bohdan

回答

1

理想情況下,你可以通過12.19 Window Functions來實現你所需要的,但是MySQL目前沒有官方的支持。

例如,在MariaDB的(> = 10.2.0),Window Functions,可以運行下面這樣的查詢:

SELECT 
    `products`.`supplier_catalog_number`, 
    `prices`.`price`, 
    ROW_NUMBER() OVER (PARTITION BY `products`.`supplier_catalog_number`) - 1 `price_index` 
FROM 
    `products` 
    INNER JOIN `prices` ON `prices`.`product_id` = `products`.`id` 
ORDER BY 
    `products`.`supplier_catalog_number`, 
    `price_index` 
LIMIT 10; 

參見db<>fiddle

一種方式來實現你所需要的(MySQL的)是執行下面的查詢,但是,它不是實現它的唯一或有效途徑:

mysql> SELECT VERSION(); 
+-----------+ 
| VERSION() | 
+-----------+ 
| 5.7.19 | 
+-----------+ 
1 row in set (0.00 sec) 

mysql> DROP TABLE IF EXISTS `prices`, `products`; 
Query OK, 0 rows affected (0.00 sec) 

mysql> CREATE TABLE IF NOT EXISTS `products` (
    -> `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, 
    -> `supplier_catalog_number` VARCHAR(12) NOT NULL 
    ->); 
Query OK, 0 rows affected (0.00 sec) 

mysql> CREATE TABLE IF NOT EXISTS `prices` (
    -> `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, 
    -> `price` DECIMAL(9, 4) NOT NULL, 
    -> `product_id` BIGINT UNSIGNED NOT NULL, 
    -> FOREIGN KEY (`product_id`) 
    ->  REFERENCES `products`(`id`) 
    ->); 
Query OK, 0 rows affected (0.00 sec) 

mysql> INSERT INTO `products` 
    -> (`supplier_catalog_number`) 
    -> VALUES 
    -> ('00000001600L'), 
    -> ('0023-200'), 
    -> ('0023-300'), 
    -> ('0023-301'); 
Query OK, 4 rows affected (0.00 sec) 
Records: 4 Duplicates: 0 Warnings: 0 

mysql> INSERT INTO `prices` 
    -> (`price`, `product_id`) 
    -> VALUES 
    -> (287.0000, 1), (243.9500, 1), 
    -> (710.0000, 2), (603.5000, 2), 
    -> (232.0000, 3), (185.6000, 3), 
    -> (289.0000, 4), (231.2000, 4), 
    -> (109.0000, 4), (92.6500, 4); 
Query OK, 10 rows affected (0.00 sec) 
Records: 10 Duplicates: 0 Warnings: 0 

mysql> SELECT 
    -> `der`.`supplier_catalog_number`, 
    -> `der`.`price`, 
    -> `der`.`price_index` 
    -> FROM (
    -> SELECT 
    ->  `products`.`supplier_catalog_number`, 
    ->  `prices`.`price`, 
    ->  (SELECT @`index` := IF(@`id` = `products`.`id`, @`index` + 1, 0)) `price_index`, 
    ->  (SELECT @`id` := `products`.`id`) 
    -> FROM 
    ->  (SELECT @`id` := 0, @`index` := 0) `init`, 
    ->  `products` 
    ->  INNER JOIN `prices` ON `prices`.`product_id` = `products`.`id` 
    -> ORDER BY 
    ->  `products`.`id`, 
    ->  `prices`.`price` 
    -> LIMIT 10 
    ->) `der` 
    -> ORDER BY 
    -> `der`.`supplier_catalog_number`, 
    -> `der`.`price_index`; 
+-------------------------+----------+-------------+ 
| supplier_catalog_number | price | price_index | 
+-------------------------+----------+-------------+ 
| 00000001600L   | 287.0000 |   0 | 
| 00000001600L   | 243.9500 |   1 | 
| 0023-200    | 710.0000 |   0 | 
| 0023-200    | 603.5000 |   1 | 
| 0023-300    | 232.0000 |   0 | 
| 0023-300    | 185.6000 |   1 | 
| 0023-301    | 289.0000 |   0 | 
| 0023-301    | 231.2000 |   1 | 
| 0023-301    | 109.0000 |   2 | 
| 0023-301    | 92.6500 |   3 | 
+-------------------------+----------+-------------+ 
10 rows in set (0.00 sec) 

db-fiddle