MySQL INSERT INTO (SELECT) with defined field - php

I am trying to copy duplicate and insert data into a table like so...
ID, Value
1,1
1,2
1,3
Now what I want to do is an insert statement that will take all rows for ID '1' and insert it in again but with an ID of 2. The ID is not unique.
Is there a way to do this without looping through all rows and inserting one by one with a regular Select, then insert statement?

Try this:
insert into TABLENAME (id,value) select 2, Value from TABLENAME where id = 1

Related

How to create duplicate values in database using id in php

Here i already have a row which contains the values, i have a id as a primary key
now what i want to do is using that id i want to duplicate the same row values and create another row.except i want to update 2 column values. how can i do this?
for ex:
in one row i already have the values say
product_id,product_price,product_nos,product_remaining,product_name
now i want to duplicate the above row with changing only one column value
,say product_remaining set value to 10.
How can i do this in php or with query?
You can use the following query if you not use auto_incrementing ids, 10 is the new value for product_remaining.
INSERT INTO table (product_price,product_nos,product_remaining,product_name)
SELECT product_price,product_nos,10,product_name FROM table
WHERE product_id = 1
or this one where you set your id, where 999 is the new id:
INSERT INTO table (product_id,product_price,product_nos,product_remaining,product_name)
SELECT 999,product_price,product_nos,10,product_name FROM table
WHERE product_id = 1

Get last inserted ID from mysql table

How to get the last inserted ID of a table, even if the last x record(s) have been deleted?
x can be 1 and can be like 200
Assuming you're talking about auto incremented IDs:
SELECT AUTO_INCREMENT FROM INFORMATION_SCHEMA.TABLES WHERE table_name = 'Table'
This will give you the next increment id. To get the last inserted id just minus 1.
Following query will give you the last id present in table
select id from table_name order by id desc limit 1;
Updated:
Then you should create table which contains two columns like table_name,
last_id and you have to always update the last_id when you insert data into particular table. With this way you can fetch the Last id inserted to table.

How to insert a id of a new row into another linking table?

I've a table with the purpose of linking two other tables with foreign keys (table 3).
table1 = id, name
table2 = id, sport
table3 = fk_table1_id, dk_table2_id
I do server side code with php and i need a way to automatic insert a row into table 3 when a row is inserted into table2.
But i dont know how to do a php query that will get the generated id of the new row in table2?(The id of table one i do have stored in a php variable)Im a looking at a smart stored procedure?
Steps to follow:
Insert a row into table2.
Find the generated key from table2 and assign it to a PHP variable.
-- read this into a php variable, say, $last_key
SELECT LAST_INSERT_ID();
Use the $last_key in insert statement for table3.
You can Follow below Steps:
1) Make ID to AUTO_INCREMENT IN Table2
2) Create Store Procedure that work as below:
A) INSERT Value to your Table2 :
B) Increase ID value : SELECT LAST_INSERT_ID() INTO VARIABLE;
C) Insert Value to your Table3 : use VARIABLE to get ID of Table2

How can I SELECT the last row that NO ID increment

How can I SELECT the last row in a MySQL table?
I'm INSERTING data and I need to retrieve a column value from the previous row.
(I'm using PHP by the way.)
the table1 something like this
table1
******************
cate_id | task_id | start_date | end_date | line |
1 2 30/04/2012 26/06/2012 text
3 1 26/06/2012 27/06/2012 text
2 1 27/06/2012 01/01/9999 text
There'sNO an auto_incrementin that table.
And my case is to update the existing last row in table and then insert a new one.
You've edited question so, here's update
SELECT MAX(cate_id) AS last_cate_id FROM table;
or you can get next ID by:
SELECT MAX(cate_id)+1 AS next_cate_id FROM table;
Without transactions this is very vulnerable for inserting same cate_id!
If you cant use them, for example because of MyISAM, you could insert with select.
INSERT INTO table
(cate_id, task_id ..)
VALUES
( (SELECT MAX(cate_id)+1 AS next_cate_id FROM table), 1 )
if you don't have order by you wont be able to get the "LAST" value or the first, because the order will not be the same (necessarily), if you don't have auto increment how can you know which one is the first or the last?, if you are working with date or auto increment you will be able to get that, however, lets say that you have a order by 'column1' you can do something like:
select * from table1 order by `column1` desc limit 1

If mysql db table does not contain row with specific ID , add data to the table

Hi i'm new to php and mysql.
I'm wondering how can i use PHP to check if a table in my mysql database contains a ROW which have the specific ID and if not add the data to the table .
For example,
Table structure:(Table name : record)
ID , DATA
i have ID=123 and DATA=hello stored in a variable in the php code , how can i use php sql query to find out whether the data exist by checking using its ID in the table , if not , INSERT the ID and DATA into the table.
I hope you understand.
p/s i have connected the php script to the database
Make the ID UNIQUE:
CREATE TABLE my_table( ID INT UNSIGNED UNIQUE...
then use INSERT IGNORE:
INSERT IGNORE INTO my_table( ID, DATA ) VALUES( some_id, some_data )
IF NOT EXISTS(SELECT 1 FROM structure WHERE ID = <yourid>)
INSERT INTO structure (ID, DATA) VALUES(<yourid>, <yourdata>)
Just replace INSERT with REPLACE.
http://dev.mysql.com/doc/refman/5.5/en/replace.html
Another option:
INSERT INTO record(id, `data`) SELECT 123, 'hello'
FROM new_table WHERE NOT EXISTS (SELECT id from record where id = 123 );
If your ID is unique key, then you can directly try to insert the row. If that ID is already in the table, the database would not let you insert it and will return error. Else you have to first run a SELECT statement to see if this ID exists in your table and if not insert it.
Also this thread will help you a lot I think How to 'insert if not exists' in MySQL?

Categories