这些是我的表格;
mysql> show tables;
+--------------------+
| Tables_in_products |
+--------------------+
| main_info |
| product1 |
| product2 |
+--------------------+
3 rows in set (0.00 sec)
这是我的第一张 table
mysql> select * from main_info;
+------+------+-------+-------+----------+
| key1 | key2 | info1 | info2 | date |
+------+------+-------+-------+----------+
| 1 | 1 | 15 | 90 | 20120501 |
| 1 | 2 | 14 | 92 | 20120601 |
| 1 | 3 | 15 | 82 | 20120801 |
| 2 | 1 | 17 | 90 | 20130302 |
| 2 | 2 | 16 | 88 | 20130601 |
+------+------+-------+-------+----------+
5 rows in set (0.00 sec)
这是产品表 1:
mysql> select * from product1;
+------+------+--------+--------------+
| key1 | key2 | serial | product_data |
+------+------+--------+--------------+
| 1 | 1 | 0 | 15.556 |
| 1 | 1 | 1 | 14.996 |
| 1 | 1 | 2 | 12.556 |
| 1 | 1 | 3 | 15.669 |
| 1 | 2 | 0 | 12.556 |
| 1 | 2 | 1 | 13.335 |
| 1 | 3 | 1 | 12.225 |
| 1 | 3 | 2 | 13.556 |
| 1 | 3 | 3 | 14.556 |
| 2 | 1 | 0 | 12.556 |
| 2 | 1 | 1 | 13.553 |
| 2 | 1 | 2 | 12.335 |
+------+------+--------+--------------+
12 rows in set (0.00 sec)
这是第二个产品表
mysql> select * from product2;
+------+------+--------+--------------+
| key1 | key2 | serial | product_data |
+------+------+--------+--------------+
| 1 | 1 | 0 | 5.556 |
| 1 | 1 | 1 | 4.996 |
| 1 | 2 | 0 | 2.556 |
| 1 | 2 | 1 | 3.335 |
| 1 | 2 | 2 | 2.56 |
| 1 | 2 | 3 | 3.556 |
| 1 | 3 | 1 | 2.225 |
| 1 | 3 | 2 | 3.556 |
| 2 | 2 | 0 | 2.556 |
| 2 | 2 | 1 | 3.553 |
+------+------+--------+--------------+
10 rows in set (0.00 sec)
我有超过 8 个产品表,我想在其中比较 2 个产品表和 main_info 表的 key1、key2 和 serial
,这取决于我想合并的最大值,如果产品数据不存在然后替换 NaN
并最终希望有 1 个输出,如下所示
预期 O/P
key1 key2 serial info1 info2 date product_table1_data product_table2_data
1 1 0 15 90 20120501 15.556 5.556
1 1 1 15 90 20120501 14.996 4.996
1 1 2 15 90 20120501 12.556 NaN
1 1 3 15 90 20120501 15.669 NaN
1 2 0 14 92 20120601 12.556 2.556
1 2 1 14 92 20120601 13.335 3.335
1 2 2 14 92 20120601 NaN 2.56
1 2 3 14 92 20120601 NaN 3.556
1 3 1 15 82 20120801 12.225 2.225
1 3 2 15 82 20120801 13.556 3.556
1 3 3 15 82 20120801 14.556 NaN
2 1 0 17 90 20130302 12.556 NaN
2 1 1 17 90 20130302 13.553 NaN
2 1 2 17 90 20130302 12.335 NaN
2 2 0 16 88 20130601 NaN 2.556
2 2 1 16 88 20130601 NaN 3.553
请有人帮我得到预期的结果
这是我的数据库结构;
$ cat product.sql
--
-- Table structure for table `main_info`
--
DROP TABLE IF EXISTS `main_info`;
CREATE TABLE `main_info` (
`key1` varchar(1000) DEFAULT NULL,
`key2` varchar(1000) DEFAULT NULL,
`info1` varchar(1000) DEFAULT NULL,
`info2` varchar(1000) DEFAULT NULL,
`date` varchar(1000) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
LOCK TABLES `main_info` WRITE;
INSERT INTO `main_info` VALUES ('1','1','15','90','20120501'),('1','2','14','92','20120601'),('1','3','15','82','20120801'),('2','1','17','90','20130302'),('2','2','16','88','20130601');
UNLOCK TABLES;
DROP TABLE IF EXISTS `product1`;
CREATE TABLE `product1` (
`key1` varchar(1000) DEFAULT NULL,
`key2` varchar(1000) DEFAULT NULL,
`serial` varchar(1000) DEFAULT NULL,
`product_data` varchar(1000) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
LOCK TABLES `product1` WRITE;
INSERT INTO `product1` VALUES ('1','1','0','15.556'),('1','1','1','14.996'),('1','1','2','12.556'),('1','1','3','15.669'),('1','2','0','12.556'),('1','2','1','13.335'),('1','3','1','12.225'),('1','3','2','13.556'),('1','3','3','14.556'),('2','1','0','12.556'),('2','1','1','13.553'),('2','1','2','12.335');
UNLOCK TABLES;
DROP TABLE IF EXISTS `product2`;
CREATE TABLE `product2` (
`key1` varchar(1000) DEFAULT NULL,
`key2` varchar(1000) DEFAULT NULL,
`serial` varchar(1000) DEFAULT NULL,
`product_data` varchar(1000) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
LOCK TABLES `product2` WRITE;
INSERT INTO `product2` VALUES ('1','1','0','5.556'),('1','1','1','4.996'),('1','2','0','2.556'),('1','2','1','3.335'),('1','2','2','2.56'),('1','2','3','3.556'),('1','3','1','2.225'),('1','3','2','3.556'),('2','2','0','2.556'),('2','2','1','3.553');
UNLOCK TABLES;
我试过了,但这并没有考虑每个 key 对的序列最大值和 NaN
SELECT *
FROM main_info INNER JOIN product1
ON
main_info.key1=product1.key1 and
main_info.key2=product1.key2
INNER JOIN product2
ON
product1.key1=product2.key1 and
product1.key2=product2.key2
合并过程细节:评论回答
1 。从 main_info 表中取出 key1 和 key2 2.查找表product1、product2,如果key1和key2在任意一个表或两个表中都存在,则获取这些key的序列列长度,求最大长度,
例如在product1表中,我们有4个序列号
+------+------+--------+--------------+
| key1 | key2 | serial | product_data |
+------+------+--------+--------------+
| 1 | 1 | 0 | 15.556 |
| 1 | 1 | 1 | 14.996 |
| 1 | 1 | 2 | 12.556 |
| 1 | 1 | 3 | 15.669 |
在第二个表中我们只有 2 个序列号,只有 0 和 1
+------+------+--------+--------------+
| key1 | key2 | serial | product_data |
+------+------+--------+--------------+
| 1 | 1 | 0 | 5.556 |
| 1 | 1 | 1 | 4.996 |
所以合并两个表中的序列号并检查数据是否存在如果存在则保留 product_date 否则写入 NaN
当你合并 2 个产品表时,输出应该是这样的,完成后添加 info1 info2 和与 key1 和 key2 对应的日期列
+------+------+--------+--------------+--------------
| key1 | key2 | serial | product_data |product_data2
+------+------+--------+--------------+-------------
| 1 | 1 | 0 | 15.556 |5.556
| 1 | 1 | 1 | 14.996 |4.996
| 1 | 1 | 2 | 12.556 |NaN
| 1 | 1 | 3 | 15.669 |NaN
如果您需要更多信息,请告诉我
谢谢。
最佳答案
试试这个
select T.key1,T.key2,T.serial,T.info1,T.info2,T.date,p1.product_data,p2.product_data
from
(SELECT m.key1 as key1,m.key2 as key2,m.info1 as info1,m.info2 as info2 ,m.date as date,p1.serial as serial
from main_info as m
join product1 as p1 on m.key1=p1.key1 and m.key2=p1.key2
union
SELECT m.key1 as key1,m.key2 as key2,m.info1 as info1,m.info2 as info2 ,m.date as date,p2.serial as serial
from main_info as m
join product2 as p2 on m.key1=p2.key1 and m.key2=p2.key2
) as T
left join product1 as p1 on T.key1=p1.key1 and T.key2=p1.key2 and p1.serial=T.serial
left join product2 as p2 on T.key1=p2.key1 and T.key2=p2.key2 and p2.serial=T.serial
编辑 或
select T.key1,T.key2,T.serial,m.info1,m.info2,m.date,p1.product_data,p2.product_data
from
(SELECT p1.key1 as key1 ,p1.key2 as key2 ,p1.serial as serial
from product1 as p1
union
SELECT p2.key1 as key1,p2.key2 as key2,p2.serial as serial
from product2 as p2
) as T
left join product1 as p1 on T.key1=p1.key1 and T.key2=p1.key2 and p1.serial=T.serial
left join product2 as p2 on T.key1=p2.key1 and T.key2=p2.key2 and p2.serial=T.serial
left join main_info as m on T.key1=m.key1 and T.key2=m.key2
关于mysql - 如何比较3个表并在mysql中合并它们的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24155835/