INSERT with SELECT in MySQL + PHP? [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to get the id of a row i've just inserted php/mysql
I was wondering what's the most efficient way of inserting something and selecting it's ID property at the same time?
For example, I have a table with auto incrementing primary ID column. I insert an item into that table and I need to know the autoincremented ID for use with another table.
Right now, what I do is:
INSERT INTO table1 the data
SELECT FROM table1 the ID
INSERT INTO table2 another set of data along with ID.

You can do this
INSERT INTO foo (auto,text)
VALUES(NULL,'text'); --generate ID by inserting NULL
INSERT INTO foo2 (id,text)
VALUES(LAST_INSERT_ID(),'text'); --use ID in second table
src: http://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html

You can try mysql_insert_id();
http://php.net/manual/en/function.mysql-insert-id.php

Related

how to move data from one table to another after some days [duplicate]

This question already has answers here:
How to store day to day records from a mysql table to another?
(2 answers)
Closed 8 years ago.
is it possible to move one table column data to another column data after 1 or 2 days i mean like we have one table which is called table1 another table is table2 table1 have some data like name contact postal address then how to move all of data from table1 selected row to table2 after a period i mean one or two and when table1 data have move to table2 it will be delete from table1 .
in more details like we insert data into table1 like entry_day column using now() we get current date is there any option to after 1 days or 2 days the data will delete from tbl1 and insert into tbl2
$q= "INSERT INTO table1 (name,contact,postal,entrydate) values ('".$row[0]."','".$row[1]."','".$row[3]."',now())";
Just create a MySQL event to handle this based on the entrydate. Its quite easy and a quick google search will turn up plenty of information on how to create them.

Is there Any Quick SQL Query to Remove Duplicate Rows [duplicate]

This question already has answers here:
MySQL remove duplicates from big database quick
(9 answers)
Closed 8 years ago.
I am trying to remove duplicate rows from my database so for that I am using this query
DELETE FROM data
WHERE data.ID NOT IN (
SELECT * FROM (
SELECT MIN(ID) FROM data GROUP BY Link
) AS p
)
It is working fine but the problem is my database has over 1 Million rows so when I use this it takes the hell of time like after 4 to 5 hours it was still at loading.. and then I just closed the tab.
So Please if someone has a better query tell me. Thanks in Advace
Table Structure
http://s29.postimg.org/bt57k5enb/image.jpg
One solution could be:
1) Create a temp table
2) Store single record for each Link column
3) Truncate "data" table
4) Alter the "data" table(add UNIQUE KEY CONSTRAINT)
5) Reimport data table back from temp table and delete tmp table
1&2) CREATE TABLE tmp AS SELECT * FROM data GROUP BY Link;
3) TRUNCATE TABLE data; -- disable foreign key constraints if any
4) ALTER TABLE data ADD UNIQUE KEY data_link_unique(Link);
5) INSERT INTO data SELECT * FROM tmp;

a more relible solution to LAST_INSERT_ID [duplicate]

This question already has answers here:
Easy mysql question regarding primary keys and an insert
(4 answers)
Closed 9 years ago.
so I want to link two tables with a common column "messageID". so first I insert into table 1
to get the Auto incremented id, then take that ID with LAST_INSERT_ID function and give that as the id for table 2:
$db->("INSERT INTO table_1 VALUES('','$message')");
$db->("INSERT INTO table_2 VALUES(LAST_INSERT_ID(),'$message');
but here's my concern, there might be two users running this script simultaneously, so in the few milliseconds between the two queries exicuting the LAST_INSERT_ID could have changed, so now the two id's are different. Is there any way I can prevent this possibility. I know it is not possible to insert into two tables with one query, which was my first thoughts. Any ideas much appreciated. Thank you
The LAST_INSERT_ID is local to the connection session, so it will not conflict with a different user making an insert.
You could try using scope_identity, which would be something like this:
$db->("DECLARE #LAST_ID int
INSERT INTO table_1 VALUES('','$message')"
SET #LAST_ID = SCOPE_IDENTITY()
);
$arg = ... $myARR['LAST_ID'] ... //however you want to get the LAST_ID from your query to PHP here
$db->("INSERT INTO table_2 VALUES(#arg,'$message');
or
$db->("DECLARE #LAST_ID int
INSERT INTO table_1 VALUES('','$message')"
SET #LAST_ID = SCOPE_IDENTITY()
INSERT INTO table_2 VALUES(#LAST_ID,'$message')
);
LAST_ID would be the value of the Auto incremented id

Get currently inserted row's id at insertion? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Retrieve inserted row ID in SQL
Is there any way to get inserted rows id right after the insertion without doing select query? Basically, the idea is like this, I have two tables, one products, and second storage which has 3 columns - id / productId / count . And I need to insert in productId the product Id of the product which I inserted right before this query. So for example -
Product with id 1.
I insert it into a table for example products, it generates id 1.
I don't do select, but right after I do another insert which will insert in table storage these data -
id - 1 / productId - 1 (took from previous query which generated id 1 with AI) / count = 0.
Hope you understood what I ment.
EDIT: I'm using MYSQL database.
Yes, in MySQL, just use the LAST_INSERT_ID() in your second query.
use mysql_insert_id(); this is a function of php it will return last inserted id of auto increment type attribute

how to select last autoincrement id using php and mysql [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
php/MySQL insert row then get 'id'
I have an inset command such as
insert (name) values($name);
I have id column as autoincrement. How can I get the id of the inserted record after inserting.
insert (name) values($name);
SET #lastid = LAST_INSERT_ID();
select blah_blah from table where id = #lastid;
To get that back into php, you do a normal mysql select on it like this:
select #lastid;
For mysql you can use mysql_insert_id to get the id of the last inserted row.

Categories