Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.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 有没有一种方法可以将变量作为列添加到SQL中的左联接之后?_Mysql_Sql_Pivot Table - Fatal编程技术网

Mysql 有没有一种方法可以将变量作为列添加到SQL中的左联接之后?

Mysql 有没有一种方法可以将变量作为列添加到SQL中的左联接之后?,mysql,sql,pivot-table,Mysql,Sql,Pivot Table,我有两张桌子: 表A: | ID | class1 | class2 | class3 | class4 | +----+-------------+---------+--------+---------+ | 1 | ABC123 | 23 | C123 | BC123 | | 2 | DEF465 | 65 | F465 | EF465 | | 3 | GHI789 | 89 | I789

我有两张桌子: 表A:

| ID |  class1     |  class2 | class3 |  class4 |
+----+-------------+---------+--------+---------+
|  1 |  ABC123     |  23     | C123   | BC123   | 
|  2 |  DEF465     |  65     | F465   | EF465   | 
|  3 |  GHI789     |  89     | I789   | HI789   |
|  4 |  JKL132     |  32     | L132   | KL132   |
|  5 |  MNO456     |  56     | O456   | NO456   |
表B:

| ID |  class_desc |     text   | 
+----+-------------+------------+
|  1 |  ABC123     |  "foo"     |  
|  2 |  23         |  "foo b"   |
|  3 |  C123       |  "foo bar" | 
|  4 |  BC123      |  "foo-bar" | 
|  5 |  DEF465     |  "bar"     | 
|  6 |  65         |  "bar f"   | 
|  7 |  F465       |  "bar foo" | 
|  7 |  EF465      |  "bar-foo" | 
etc...
我想做一个左连接,得到一个表,如表C:

| ID |  class1  |  class2 | class3 |  class4 | class_1_desc | class_2_desc | class_3_desc| class_4_desc|
+----+----------+---------+--------+---------+--------------+--------------+-------------+-------------+
|  1 |  ABC123  |  23     | C123   | BC123   | "foo"        |"foo b"       |"foo bar"    |"foo-bar"    |
|  2 |  DEF465  |  65     | F465   | EF465   | "bar"        |"bar f"       |"bar foo"    |  "bar-foo"  |
|  3 |  GHI789  |  89     | I789   | HI789   | etc...
|  4 |  JKL132  |  32     | L132   | KL132   |
|  5 |  MNO456  |  56     | O456   | NO456   |

提前感谢

您似乎想要多个
左加入
s:

select a.*,
       b1.text as text_1,
       b2.text as text_2,
       b3.text as text_3,
       b4.text as text_4
from a left join
     b b1
     on b1.class_desc = a.class1 left join
     b b2
     on b2.class_desc = a.class2 left join
     b b3
     on b3.class_desc = a.class3 left join
     b b4
     on b4.class_desc = a.class4;
您可以加入4次:

select 
    ta.*,
    tb1.text class_1_desc,
    tb2.text class_2_desc,
    tb3.text class_3_desc,
    tb4.text class_4_desc
from tableA ta
inner join tableB tb1 on tb1.class_desc = ta.class1
inner join tableB tb2 on tb2.class_desc = ta.class2
inner join tableB tb3 on tb3.class_desc = ta.class3
inner join tableB tb4 on tb4.class_desc = ta.class4
如果在
表a
中声明的
可能在
表B
中不存在,则需要
左联接
s而不是
内联接
s

如果您真的想创建一个新表,比如说
tableC
,其中包含查询结果,那么您可以使用
createtable。。。如选择
语法:

create table tableC as
select 
    ta.*,
    tb1.text class_1_desc,
    tb2.text class_2_desc,
    tb3.text class_3_desc,
    tb4.text class_4_desc
from tableA ta
inner join tableB tb1 on tb1.class_desc = ta.class1
inner join tableB tb2 on tb2.class_desc = ta.class2
inner join tableB tb3 on tb3.class_desc = ta.class3
inner join tableB tb4 on tb4.class_desc = ta.class4
(由于示例数据不完整,请使用
左连接):

ID | 1类| 2类| 3类| 4类| 1类| 2类| 3类| 4类| -: | :----- | -----: | :----- | :----- | :----------- | :----------- | :----------- | :----------- 1 | ABC123 | 23 | C123 | foo | foo b | foo bar | foo bar 2 | DEF465 | 65 | F465 | EF465 | bar | bar f | bar foo | null 3 | GHI789 | 89 | I789 | HI789 | null | null | null | null 4 | JKL132 | 32 | L132 | KL132 | null | null | null | null 5 | MNO456 | 56 | O456 | NO456 | null | null | null | null
欢迎来到SO。请注意,“etc”在这种情况下很少有用。相反,请参见 ID | class1 | class2 | class3 | class4 | class_1_desc | class_2_desc | class_3_desc | class_4_desc -: | :----- | -----: | :----- | :----- | :----------- | :----------- | :----------- | :----------- 1 | ABC123 | 23 | C123 | BC123 | foo | foo b | foo bar | foo-bar 2 | DEF465 | 65 | F465 | EF465 | bar | bar f | bar foo | null 3 | GHI789 | 89 | I789 | HI789 | null | null | null | null 4 | JKL132 | 32 | L132 | KL132 | null | null | null | null 5 | MNO456 | 56 | O456 | NO456 | null | null | null | null