我应该如何在MySQL中编写查询以获取从一个表到另一个表的值

时间:2022-09-21 15:56:18

I want some field datas to get inserted from a table(called profile) to another table(services) along with those field datas in the table(to which the values want to be inserted,i.e. services).But I am failing. I am new to MySQL and database queries i need ur help.

我想要一些字段数据从表(称为配置文件)插入到另一个表(服务)以及表中的那些字段数据(值要插入的数据,即服务)。但是我失败了。我是MySQL和数据库查询的新手我需要你的帮助。

  $qur = mysql_query("INSERT INTO services(FirstName,LastName,DOB,Mobile,email,CountryCode,Address,State,City,Country,PinCode,altmobnumber,PanCard,AdharCard,ServiceOffering,Fee,FeeDuration,FeeExtraHour,negotiable) select profile.FirstName,profile.LastName,profile.DOB,profile.Mobile,profile.email,profile.CountryCode,profile.Address,profile.State,profile.City,profile.Country,profile.PinCode from profile where id = '$id'")or die(mysql_error());

1 个解决方案

#1


1  

You need to specify value for every column in your VALUES list. If you do not have any value from the SELECT statement, either use a NULL value for respective field or simply remove it from the VALUES list.

您需要为VALUES列表中的每一列指定值。如果SELECT语句中没有任何值,则对相应字段使用NULL值,或者只是从VALUES列表中删除它。

e.g. if you want to insert three values in tbl1 but tbl2 contains only 2 fields, use a NULL value for the third field

例如如果要在tbl1中插入三个值但tbl2仅包含2个字段,请对第三个字段使用NULL值

INSERT INTO tbl1 (a, b, c) SELECT tbl2.a, tbl2.b, NULL FROM tbl2

or if you have a hard coded value,

或者如果您有硬编码值,

INSERT INTO tbl1 (a, b, c) SELECT tbl2.a, tbl2.b, some_value FROM tbl2

or simply use only two fields

或者只使用两个字段

INSERT INTO tbl1 (a, b) SELECT tbl2.a, tbl2.b FROM tbl2

Now, the VALUES part of your query has 19 fields but your SELECT statement provides only 11 fields. Either add NULL for the last 8 fields for successful execution or remove the last 8 fields from VALUES list

现在,查询的VALUES部分有19个字段,但SELECT语句只提供11个字段。要么为最后8个字段添加NULL以便成功执行,要么从VALUES列表中删除最后8个字段

#1


1  

You need to specify value for every column in your VALUES list. If you do not have any value from the SELECT statement, either use a NULL value for respective field or simply remove it from the VALUES list.

您需要为VALUES列表中的每一列指定值。如果SELECT语句中没有任何值,则对相应字段使用NULL值,或者只是从VALUES列表中删除它。

e.g. if you want to insert three values in tbl1 but tbl2 contains only 2 fields, use a NULL value for the third field

例如如果要在tbl1中插入三个值但tbl2仅包含2个字段,请对第三个字段使用NULL值

INSERT INTO tbl1 (a, b, c) SELECT tbl2.a, tbl2.b, NULL FROM tbl2

or if you have a hard coded value,

或者如果您有硬编码值,

INSERT INTO tbl1 (a, b, c) SELECT tbl2.a, tbl2.b, some_value FROM tbl2

or simply use only two fields

或者只使用两个字段

INSERT INTO tbl1 (a, b) SELECT tbl2.a, tbl2.b FROM tbl2

Now, the VALUES part of your query has 19 fields but your SELECT statement provides only 11 fields. Either add NULL for the last 8 fields for successful execution or remove the last 8 fields from VALUES list

现在,查询的VALUES部分有19个字段,但SELECT语句只提供11个字段。要么为最后8个字段添加NULL以便成功执行,要么从VALUES列表中删除最后8个字段