Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

update a table using a subselect in a query?

Status
Not open for further replies.

Riverman1111

Programmer
Feb 11, 2009
15
US
Hey,

Does anyone know the syntax to insert a row to a table using a sub-select on the same table using the primary key? I know how to do it in MS Sql Server, can't do in MySql.

Sql Server syntax is:

INSERT TABLE
SELECT FIELD1,
FIELD2
FROM TABLE A
WHERE TABLE_ID = @TABLE_ID
 
The mysql online documentation is your friend:



Code:
INSERT INTO tablename (field1,field2,field3) SELECT field1,field2,field3 FROM tablename WHERE id=xxx





----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
That syntax gives me the error message:

'column count doesn't match value count at row 1'

???
 
Then read the error message. It says that the SELECT part of the query does not have the same amount of fields as the INSERT part. In other words: you forgot a column in either the SELECT or the INSERT part.

+++ Despite being wrong in every important aspect, that is a very good analogy +++
Hex (in Darwin's Watch)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top