Copy and insert data in the same table in mysql

1. Syntax

:

--


	INSERT INTO table_name1 (
		column_1,
		column_2,
		column_3,
		column_4
	)

	SELECT
		column_1,
		column_2,
		column_3,
		column_4
	FROM
		table_name1
	WHERE
		id = 'xxx'


--



2. Note that

   if it is the primary key, count(*) can be used as the primary key.



3. Example



INSERT INTO tb_users(
	_id,
	_username,
	_password,
	_address,
	_age,
	_email
)
SELECT

	(select count(*) from tb_users),
	_username,
	_password,
	_address,
	_age,
	_email
FROM
	tb_users
WHERE
	F_KEY = '30c0d5ad4c650ff84ed01c4d9e7e44ef'




4. Using the concat function

   You can also use concat((select count(*) from tb_users), "AAAA_")
   to set the id to start with AAAA_.

















-Please

indicate the reference, the
original source: http://lixh1986.iteye.com/blog/2363370

--

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326332984&siteId=291194637