MySQL: 按特定顺序连接两个表的不同行?
MySQL: Join distinct rows of two tables in a certain order?
我有一份我想要的库存单位和销售交易清单,(1) 按单位 SKU 加入,(2) 按日期以先进先出的顺序将一项交易关联到一个库存单位.我在第二部分遇到问题。
我能想到的最好的是:
SELECT `units`.`unit_date`, `units`.`unit_id`, `trans`.`tran_date`, `trans`.`tran_id`, `units`.`unit_sku` FROM `units`
INNER JOIN `trans`
ON `trans`.`unit_sku` = `units`.`unit_sku`
GROUP BY `trans`.`tran_id`, `trans`.`unit_sku`
ORDER BY `units`.`unit_date` asc, `trans`.`tran_date` asc
;
units
table:
unit_date | unit_id | unit_sku
2015-06-01 | 1 | U1KLM
2015-06-02 | 2 | U1KLM
2015-06-03 | 3 | U2QRS
2015-06-04 | 4 | U2QRS
2015-06-05 | 5 | U1KLM
trans
table:
tran_date | tran_id | unit_sku
2015-06-11 | A | U2QRS
2015-06-12 | B | U1KLM
2015-06-13 | C | U1KLM
2015-06-14 | D | U2QRS
2015-06-15 | E | U1KLM
想要的结果 是一个 tran_id
与 unit_sku
中的一个 unit_id
按从早到晚的顺序加入unit_date
:
unit_date | unit_id | tran_date | tran_id | unit_sku
2015-06-01 | 1 | 2015-06-12 | B | U1KLM
2015-06-02 | 2 | 2015-06-13 | C | U1KLM
2015-06-03 | 3 | 2015-06-11 | A | U2QRS
2015-06-04 | 4 | 2015-06-14 | D | U2QRS
2015-06-05 | 5 | 2015-06-15 | E | U1KLM
查询(不需要的)结果仅将tran_id
连接到最早出现的unit_sku
的unit_id
:
unit_date | unit_id | tran_date | tran_id | unit_sku
2015-06-01 | 1 | 2015-06-12 | B | U1KLM
2015-06-01 | 1 | 2015-06-13 | C | U1KLM
2015-06-01 | 1 | 2015-06-15 | E | U1KLM
2015-06-03 | 3 | 2015-06-11 | A | U2QRS
2015-06-03 | 3 | 2015-06-14 | D | U2QRS
关于如何获得预期结果的任何想法?在此设置中,只有 unit_date
和 tran_date
是 sortable;其余随机生成。
复制脚本:
DROP TEMPORARY TABLE IF EXISTS `units`;
DROP TEMPORARY TABLE IF EXISTS `trans`;
CREATE TEMPORARY TABLE `units` (`unit_date` date, `unit_id` char(1) , `unit_sku` char(5), PRIMARY KEY(`unit_id`));
CREATE TEMPORARY TABLE `trans` (`tran_date` date, `tran_id` char(1) , `unit_sku` char(5), PRIMARY KEY(`tran_id`));
INSERT INTO `units` (`unit_date`, `unit_id`, `unit_sku`) VALUES
('2015-06-01', '1', 'U1KLM')
, ('2015-06-02', '2', 'U1KLM')
, ('2015-06-03', '3', 'U2QRS')
, ('2015-06-04', '4', 'U2QRS')
, ('2015-06-05', '5', 'U1KLM')
;
INSERT INTO `trans` (`tran_date`, `tran_id`, `unit_sku`) VALUES
('2015-06-11', 'A', 'U2QRS')
, ('2015-06-12', 'B', 'U1KLM')
, ('2015-06-13', 'C', 'U1KLM')
, ('2015-06-14', 'D', 'U2QRS')
, ('2015-06-15', 'E', 'U1KLM')
;
SELECT `units`.`unit_date`, `units`.`unit_id`, `trans`.`tran_date`, `trans`.`tran_id`, `units`.`unit_sku` FROM `units`
INNER JOIN `trans`
ON `trans`.`unit_sku` = `units`.`unit_sku`
GROUP BY `trans`.`tran_id`, `trans`.`unit_sku`
ORDER BY `units`.`unit_date` asc, `trans`.`tran_date` asc
;
我相信这就是您要找的:(这是假设 1 对 1 的关系)
SET @UNITRN := 0;
SET @TRANSRN :=0;
SELECT A.`unit_date`, A.`unit_id`, B.`tran_date`, B.`tran_id`, A.`unit_sku` FROM (SELECT @UNITRN := @UNITRN + 1 AS ROWNUM, UNIT_DATE, UNIT_ID, UNIT_SKU FROM UNITS ORDER BY UNIT_SKU, UNIT_DATE ASC) A
JOIN (SELECT @TRANSRN := @TRANSRN + 1 AS ROWNUM, TRAN_DATE, TRAN_ID, UNIT_SKU FROM TRANS ORDER BY UNIT_SKU, TRAN_DATE ASC) B
ON A.UNIT_SKU = B.UNIT_SKU
AND A.ROWNUM = B.ROWNUM
ORDER BY A.UNIT_DATE ASC
我有一份我想要的库存单位和销售交易清单,(1) 按单位 SKU 加入,(2) 按日期以先进先出的顺序将一项交易关联到一个库存单位.我在第二部分遇到问题。
我能想到的最好的是:
SELECT `units`.`unit_date`, `units`.`unit_id`, `trans`.`tran_date`, `trans`.`tran_id`, `units`.`unit_sku` FROM `units`
INNER JOIN `trans`
ON `trans`.`unit_sku` = `units`.`unit_sku`
GROUP BY `trans`.`tran_id`, `trans`.`unit_sku`
ORDER BY `units`.`unit_date` asc, `trans`.`tran_date` asc
;
units
table:
unit_date | unit_id | unit_sku
2015-06-01 | 1 | U1KLM
2015-06-02 | 2 | U1KLM
2015-06-03 | 3 | U2QRS
2015-06-04 | 4 | U2QRS
2015-06-05 | 5 | U1KLM
trans
table:
tran_date | tran_id | unit_sku
2015-06-11 | A | U2QRS
2015-06-12 | B | U1KLM
2015-06-13 | C | U1KLM
2015-06-14 | D | U2QRS
2015-06-15 | E | U1KLM
想要的结果 是一个 tran_id
与 unit_sku
中的一个 unit_id
按从早到晚的顺序加入unit_date
:
unit_date | unit_id | tran_date | tran_id | unit_sku
2015-06-01 | 1 | 2015-06-12 | B | U1KLM
2015-06-02 | 2 | 2015-06-13 | C | U1KLM
2015-06-03 | 3 | 2015-06-11 | A | U2QRS
2015-06-04 | 4 | 2015-06-14 | D | U2QRS
2015-06-05 | 5 | 2015-06-15 | E | U1KLM
查询(不需要的)结果仅将tran_id
连接到最早出现的unit_sku
的unit_id
:
unit_date | unit_id | tran_date | tran_id | unit_sku
2015-06-01 | 1 | 2015-06-12 | B | U1KLM
2015-06-01 | 1 | 2015-06-13 | C | U1KLM
2015-06-01 | 1 | 2015-06-15 | E | U1KLM
2015-06-03 | 3 | 2015-06-11 | A | U2QRS
2015-06-03 | 3 | 2015-06-14 | D | U2QRS
关于如何获得预期结果的任何想法?在此设置中,只有 unit_date
和 tran_date
是 sortable;其余随机生成。
复制脚本:
DROP TEMPORARY TABLE IF EXISTS `units`;
DROP TEMPORARY TABLE IF EXISTS `trans`;
CREATE TEMPORARY TABLE `units` (`unit_date` date, `unit_id` char(1) , `unit_sku` char(5), PRIMARY KEY(`unit_id`));
CREATE TEMPORARY TABLE `trans` (`tran_date` date, `tran_id` char(1) , `unit_sku` char(5), PRIMARY KEY(`tran_id`));
INSERT INTO `units` (`unit_date`, `unit_id`, `unit_sku`) VALUES
('2015-06-01', '1', 'U1KLM')
, ('2015-06-02', '2', 'U1KLM')
, ('2015-06-03', '3', 'U2QRS')
, ('2015-06-04', '4', 'U2QRS')
, ('2015-06-05', '5', 'U1KLM')
;
INSERT INTO `trans` (`tran_date`, `tran_id`, `unit_sku`) VALUES
('2015-06-11', 'A', 'U2QRS')
, ('2015-06-12', 'B', 'U1KLM')
, ('2015-06-13', 'C', 'U1KLM')
, ('2015-06-14', 'D', 'U2QRS')
, ('2015-06-15', 'E', 'U1KLM')
;
SELECT `units`.`unit_date`, `units`.`unit_id`, `trans`.`tran_date`, `trans`.`tran_id`, `units`.`unit_sku` FROM `units`
INNER JOIN `trans`
ON `trans`.`unit_sku` = `units`.`unit_sku`
GROUP BY `trans`.`tran_id`, `trans`.`unit_sku`
ORDER BY `units`.`unit_date` asc, `trans`.`tran_date` asc
;
我相信这就是您要找的:(这是假设 1 对 1 的关系)
SET @UNITRN := 0;
SET @TRANSRN :=0;
SELECT A.`unit_date`, A.`unit_id`, B.`tran_date`, B.`tran_id`, A.`unit_sku` FROM (SELECT @UNITRN := @UNITRN + 1 AS ROWNUM, UNIT_DATE, UNIT_ID, UNIT_SKU FROM UNITS ORDER BY UNIT_SKU, UNIT_DATE ASC) A
JOIN (SELECT @TRANSRN := @TRANSRN + 1 AS ROWNUM, TRAN_DATE, TRAN_ID, UNIT_SKU FROM TRANS ORDER BY UNIT_SKU, TRAN_DATE ASC) B
ON A.UNIT_SKU = B.UNIT_SKU
AND A.ROWNUM = B.ROWNUM
ORDER BY A.UNIT_DATE ASC