我正在尝试创建一个查询,将一个现有表中的数据输入到另一个现有表中。两者之间没有共同的id字段。
我有以下现有表t1
class EasyFile{
std::string fileContent;
char* filePath;
public:
EasyFile(char* filePath){
this->filePath = filePath;
std::fstream file(filePath);
getline(file,fileContent);
}
char* getFilePath(){
return filePath;
}
std::string getFileContent(){
return fileContent;
}
void setContent(std::string content,char* filePath){
std::ofstream file(filePath);
file<<content;
}
void operator=(EasyFile f);
};
void EasyFile::operator=(EasyFile f){
this->setContent(f.getFileContent(),f.getFilePath());
}
int main(int argc,char** argv)
{
EasyFile efile1(argv[1]);
EasyFile efile2(argv[2]);
efile2 = efile1;
std::string output="";
std::ifstream file(argv[2]);
std::getline(file,output);
std::cout<<output;
return 0;
}
有大量用户和(大约100个标准中)6个标准 我有兴趣插入以下现有表格
----------+------+-------+
|user | criteria | record|
----------+------+-------+
| 1 | 11 | K |
----------+------+-------+
| 1 | 12 | L |
----------+------+-------+
| 1 | 13 | M |
----------+------+-------+
| 1 | 16 | P |
----------+------+-------+
| 1 | 18 | R |
----------+------+-------+
| 1 | 20 | T |
----------+------+-------+
| 2 | 11 | K |
----------+------+-------+
| 2 | 12 | L |
----------+------+-------+
| 2 | 13 | M |
----------+------+-------+
| 2 | 16 | P |
----------+------+-------+
| 2 | 18 | R |
----------+------+-------+
| 2 | 20 | T |
----------+------+-------+
t1用户与t2中用户的列相同。从t1为t1用户插入的数据必须匹配t2中已存在的同一用户的数据。
我的最终查询就是这样(这不起作用)
table t2
+----------+----------+----------+----------+----------+----------+
| Label u | Label v | Label w | Label x | Label y | Label z |
+----------+----------+----------+----------+----------+----------+
| record K | record L | record M | record P | record R | record T|
| record K | record L | record M | record P | record R | record T|
+----------+----------+----------+----------+----------+----------+
where Criteria number 11 = Label u
Criteria number 12 = Label v
Criteria number 13 = Label w
Criteria number 16 = Label x
Criteria number 18 = Label y
Criteria number 20 = Label z
Note* Line 1 in t2 corresponds to user 1
Line 2 in t2 corresponds to user 2
There is no "user" column in t2 for the fields "user 1, user 2"
t2 already contains data in other columns
答案 0 :(得分:0)
这是您的数据透视表。如果您不想拥有用户ID,可以删除2.行
SELECT
user,
GROUP_CONCAT(IF (criteria = 11,record,NULL)) AS Label_u,
GROUP_CONCAT(IF (criteria = 12,record,NULL)) AS Label_v,
GROUP_CONCAT(IF (criteria = 13,record,NULL)) AS Label_w,
GROUP_CONCAT(IF (criteria = 14,record,NULL)) AS Label_x,
GROUP_CONCAT(IF (criteria = 15,record,NULL)) AS Label_y,
GROUP_CONCAT(IF (criteria = 16,record,NULL)) AS Label_z
FROM mytable
WHERE user IN (1,2)
GROUP BY USER
ORDER BY USER;
结果
+------+---------+---------+---------+---------+---------+---------+
| user | Label_u | Label_v | Label_w | Label_x | Label_y | Label_z |
+------+---------+---------+---------+---------+---------+---------+
| 1 | P1 | P1 | P1 | P1 | R1 | T1 |
| 2 | P2 | P2 | P2 | P2 | R2 | T2 |
+------+---------+---------+---------+---------+---------+---------+
2 rows in set (0.00 sec)
MariaDB >
答案 1 :(得分:0)
为了设计这个查询,可以从tc和tr同时更新其他字段 - 这个答案适用于tc.a = tr.cid和tr.cid = t1.user
的地方SELECT ' Label_u'
,' Label_u'
,' Label_v'
,' Label_w'
,' Label_x'
,' Label_y'
,' Label_z'
,(SELECT `record` FROM `t1` WHERE `user` = b.id AND `criteria` = 11)
,(SELECT `record` FROM `t1` WHERE `user` = b.id AND `criteria` = 12)
,(SELECT `record` FROM `t1` WHERE `user` = b.id AND `criteria` = 13)
,(SELECT `record` FROM `t1` WHERE `user` = b.id AND `criteria` = 16)
,(SELECT `record` FROM `t1` WHERE `user` = b.id AND `criteria` = 18)
,(SELECT `record` FROM `t1` WHERE `user` = b.id AND `criteria` = 20)
FROM tc a
, tr b
WHERE a .id = b .cid
AND b .print_id IS NOT NULL ;