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

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

Related

MySql: How to Delete rows from a table EXCEPT a row with specific ID [duplicate]

This question already has answers here:
SQL WHERE condition is not equal to?
(10 answers)
Closed 2 years ago.
I have an html table with checkboxes. Users are permitted to delete rows from that html table except one specific one, whose value is hard coded in the DB.
If a user accidently checks it regardless, I want my server code (php) or even better the MySqL to run a delete query for all the rows they checked EXCEPT for that 1 specific row. So,something like this( which is wrong):
DELETE FROM table_name
WHERE row_id = some_value
EXCEPT row_id = some_value;
Many thanks !
I think the logic you want is AND:
DELETE FROM table_name
WHERE row_id = some_value AND row_id <> some_value;
Of course, some_value should be different most of the time.
DELETE FROM table_name WHERE row_id NOT IN (do_not_delete_this_value );
do_not_delete_this_value will be the id of the row you don't want to delete, rest all records will get deleted.
In fact there are a couple of ways to do it, for deleting all row's except one specific ID, type the following command in SQL query:
In between parentheses, type the ID number that you want to keep.
DELETE FROM `table_name` WHERE `ID` NOT IN (1)
Also, to delete all records except some id's, use this command:
DELETE FROM `table_name` WHERE `ID` NOT IN (1,2,6,10)

Selecting the most recently added record to the database in Mysql [duplicate]

This question already has answers here:
PostgreSQL, SELECT from max id
(5 answers)
Closed 4 years ago.
I have a table in database which has a primary key called id. Now i want to display the most recently added 2 records into the table.
Assuming id is some sort of auto-incrementing integer value then the following will work
SELECT * FROM table ORDER BY id DESC LIMIT 2;
If you want just the last inserted record id (again assuming the insert generates an autoincrement id) there is also LAST_INSERT_ID. But beware this is global and will return last inserted id database-wide so it is not often used in SELECTS but rather as an OUT paramer in a routine to return the id of the just-inserted row.
SELECT LAST_INSERT_ID();
This wil definitely work,
SELECT * FROM table_name ORDER BY id DESC,LIMIT 2.
Try,
SELECT * FROM `table`
ORDER BY id
DESC
LIMIT 0, 2;

SQL query using replace a value in one table by randomly selecting a value in another table

I have a table with an value that I would like to be updated periodically with a cron job. However, I need to update the value by replacing it with a value from a different table. The issue is that I would like the replacement value to be chosen randomly.
For example, Table 1 has
ID Email
=================
1 bobatumail
Table 2 has:
ID Email
================
1 bobatumail
2 joeatumail
3 peteatumail
4 biffatumail
5 wilneratumail
6 wilsonatumail
I would like the query to replace bobatumail in Table 1 with any of the other values in Table 2 as long as it is random. It could even be the same value as in Table 1.
Any idea how to do this?
In MySQL you could use the REPLACE statement:
REPLACE INTO table1 (ID, Email)
SELECT 1, table2.Email FROM table2 ORDER BY RAND() LIMIT 1;
The "1" in the second line represents the id of the entry while the second part returns a random value out of table2. Yes, there are solutions using the UPDATE statement (JOIN and ANSI) but its always tricky and you usually have to turn off safe update mode.
http://dev.mysql.com/doc/refman/5.5/en/mysql-command-options.html#option_mysql_safe-updates
Please note that REPLACE first deletes the old entry and then reinserts the new one.
http://dev.mysql.com/doc/refman/5.5/en/replace.html

INSERT with SELECT in MySQL + PHP? [duplicate]

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

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