简体   繁体   English

MySQL-将具有不同行值的行列值从一个表复制到另一张表(2次)

[英]Mysql - Copy row colum values from one table to another table (2 times) with different row values

I have been trying this query but its giving me an error. 我一直在尝试此查询,但它给我一个错误。 I believe the query is right, but i 'm not getting the desired result as i wanted. 我相信查询是正确的,但是我没有得到想要的结果。 Any inputs or guidance would be greatly appreciated. 任何输入或指导将不胜感激。 The following queries work only if i run the first query, but when i run the first and second query, it gives me error! 以下查询仅在我运行第一个查询时有效,但是当我运行第一个和第二个查询时,它给我错误!

queries:
insert into user_tbl(firstname, lastname, location) 
select `fname`, `lname`, `location` from temp_tbl

insert into user_tbl(location) 
select `location2`from ok

If i run the first query, it copies from temp_tbl to user_tbl without any error 如果我运行第一个查询,它将从temp_tbl复制到user_tbl,没有任何错误

Only first query output!
--------------------------------
firstname, lastname, location
john       doe        New York
jane       doe        Mexico

This is my desired output: 这是我想要的输出:

user_tbl:
--------------------------------
firstname, lastname, location
john       doe        New York
                      Manhattan
jane       doe        Mexico
                      Bahamas

I don't know if there's any other query im missing here, but the query above is what i've come up with. 我不知道这里是否还有其他查询,但是上面的查询是我提出的。 So, basically what i want is whenever a single row columns values from temp_table table to user_tbl table is copied, i want a new row inserted for that same row. 因此,基本上我想要的是每当从temp_table表到user_tbl表的单行列值被复制时,我想要为该行插入新行。

Thanks in advance 提前致谢

why would you insert a row with no first and last name? 为什么要插入没有名字和姓氏的行? I don't think you've architected correctly. 我认为您的架构设计不正确。 as far as reasons it failed, what is the error? 至于失败的原因,这是什么错误? are the the first and lastname required fields maybe? 姓氏和名字必填字段?

You should verify if the attributes "firstname" and "lastname" in the table user_tbl can accept null value. 您应该验证表user_tbl中的属性“ firstname”和“ lastname”是否可以接受空值。 If these attributes are not null it is normal that you have an error when you try to insert only "location" in this table. 如果这些属性不为null,则在尝试仅在此表中插入“位置”时出现错误是正常的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM