如何比较3个表并在mysql中合并它们的列

时间:2014-06-11 06:17:38

标签: mysql database

Sqlfiddle

这些是我的表格;

         mysql> show tables;
         +--------------------+
         | Tables_in_products |
         +--------------------+
         | main_info          |
         | product1           |
         | product2           |
         +--------------------+
         3 rows in set (0.00 sec)

这是我的第一张表

         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 and 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;

我试过这个,但是这并没有考虑每个密钥对的序列最大值以及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,则获取这些键的串行列的长度,找到最大长度,

例如在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个产品表时输出应该是这样的,一旦完成,添加对应于key1和key2的info1 info2和date列

     +------+------+--------+--------------+--------------
     | 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

如果您需要更多信息,请与我们联系

谢谢。

2 个答案:

答案 0 :(得分:1)

试试这个

 SELECT  * FROM ( SELECT main_info.* ,
 product1.product_data as product_table1_data,
 IF(product2.product_data IS NULL,"NaaN", 
 product2.product_data)  as product_table2_data, product1.serial 
 FROM product1 
 LEFT JOIN product2 ON product1.key1 = product2.key1 
 AND product1.key2 = product2.key2 
 AND product1.serial=product2.serial 
 INNER JOIN main_info on 
 main_info.key1=product1.key1 and 
 main_info.key2=product1.key2 

 UNION 

 SELECT main_info.* , 
 IF(product1.product_data IS NULL,"NaaN",product1.product_data) as product_table1_data,
 product2.product_data  as product_table2_data, 
 product2.serial 
 FROM product1 RIGHT JOIN product2 ON product1.key1 = product2.key1 
 AND product1.key2 = product2.key2 
 AND product1.serial=product2.serial 
 INNER JOIN main_info on 
 main_info.key1=product2.key1 and 
 main_info.key2=product2.key2) AS TEMP ORDER BY TEMP.key1,TEMP.key2,TEMP.serial 

答案 1 :(得分:1)

试试这个

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