Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MYSQL:从其他表插入数据_Mysql_Sql_Case - Fatal编程技术网

MYSQL:从其他表插入数据

MYSQL:从其他表插入数据,mysql,sql,case,Mysql,Sql,Case,我正在尝试插入另外两个表中的数据。我目前有两个表,t1和t2: t1: 和t2: A B C col1 4 99 81 col2 50 26 38 col3 36 38 11 col4 16 49 70 col5 42 83 93 我的目标是形成一个新表,该表仅使用t1和t2中的数据,并形成一个新表,如: col1 col2 col3 col4 col5 4 50 38 16

我正在尝试插入另外两个表中的数据。我目前有两个表,t1和t2:

t1:

和t2:

         A   B   C
col1     4   99  81
col2     50  26  38
col3     36  38  11
col4     16  49  70
col5     42  83  93
我的目标是形成一个新表,该表仅使用t1和t2中的数据,并形成一个新表,如:

col1  col2  col3  col4  col5
 4     50    38    16    93
 99    26    11    70    42
 99    38    38    49    42
 81    50    36    16    83
我一直在使用CASE函数,但在附加表时遇到了问题。有什么建议吗


谢谢

如果我们将
t2
从宽表转换为长表:

mysql> CREATE VIEW tidyt2 AS
       SELECT x as 'col', 'A' as 'label', A 'value' FROM t2
       UNION SELECT x as 'col', 'B' as 'label', B 'value' FROM t2
       UNION SELECT x as 'col', 'C' as 'label', C 'value' FROM t2;

Query OK, 0 rows affected (0.03 sec)

mysql> select * from tidyt2;
+------+-------+-------+
| col  | label | value |
+------+-------+-------+
| col1 | A     |     4 |
| col2 | A     |    50 |
| col3 | A     |    36 |
| col4 | A     |    16 |
| col5 | A     |    42 |
| col1 | B     |    99 |
| col2 | B     |    26 |
| col3 | B     |    38 |
| col4 | B     |    49 |
| col5 | B     |    83 |
| col1 | C     |    81 |
| col2 | C     |    38 |
| col3 | C     |    11 |
| col4 | C     |    70 |
| col5 | C     |    93 |
+------+-------+-------+
15 rows in set (0.00 sec)
然后,可以使用左联接表示所需的表:

mysql> SELECT t21.value as 'col1'
              , t22.value as 'col2'
              , t23.value as 'col3'
              , t24.value as 'col4'
              , t25.value as 'col5'
         FROM t1 
         LEFT JOIN tidyt2 as t21 ON t1.col1 = t21.label AND t21.col='col1'
         LEFT JOIN tidyt2 as t22 ON t1.col2 = t22.label AND t22.col='col2'
         LEFT JOIN tidyt2 as t23 ON t1.col3 = t23.label AND t23.col='col3'
         LEFT JOIN tidyt2 as t24 ON t1.col4 = t24.label AND t24.col='col4'
         LEFT JOIN tidyt2 as t25 ON t1.col5 = t25.label AND t25.col='col5';

+------+------+------+------+------+
| col1 | col2 | col3 | col4 | col5 |
+------+------+------+------+------+
|    4 |   50 |   38 |   16 |   93 |
|   99 |   26 |   11 |   70 |   42 |
|   99 |   38 |   38 |   49 |   42 |
|   81 |   50 |   36 |   16 |   83 |
+------+------+------+------+------+
4 rows in set (0.00 sec)

你能在编程语言层面上解决这个问题吗?。有时mysql会承受太大的压力。我使用的语言(python)可以解决这个问题,但mysql是我的第一选择。为什么需要将数据存储在新表中。你为什么不选择你拥有的数据呢?Dan,我想稍后使用新表,并认为形成完整的新表会更容易。我完全是sql新手,也许多做一张表是愚蠢的。但是,如何在这里选择数据有什么建议吗?谢谢丹和罗曼的回复!我认为这可以很容易地用R解决。
mysql> SELECT t21.value as 'col1'
              , t22.value as 'col2'
              , t23.value as 'col3'
              , t24.value as 'col4'
              , t25.value as 'col5'
         FROM t1 
         LEFT JOIN tidyt2 as t21 ON t1.col1 = t21.label AND t21.col='col1'
         LEFT JOIN tidyt2 as t22 ON t1.col2 = t22.label AND t22.col='col2'
         LEFT JOIN tidyt2 as t23 ON t1.col3 = t23.label AND t23.col='col3'
         LEFT JOIN tidyt2 as t24 ON t1.col4 = t24.label AND t24.col='col4'
         LEFT JOIN tidyt2 as t25 ON t1.col5 = t25.label AND t25.col='col5';

+------+------+------+------+------+
| col1 | col2 | col3 | col4 | col5 |
+------+------+------+------+------+
|    4 |   50 |   38 |   16 |   93 |
|   99 |   26 |   11 |   70 |   42 |
|   99 |   38 |   38 |   49 |   42 |
|   81 |   50 |   36 |   16 |   83 |
+------+------+------+------+------+
4 rows in set (0.00 sec)