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
Related
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)
This question already has answers here:
Can I use a subquery inside an INSERT statement?
(2 answers)
Closed 8 years ago.
I need to insert some values in the table.
I can do it easily using the query(from php):
mysql_query('INSERT INTO `TABLE1`(`user_id`,`value1`,`date`) VALUES("'.$user_id.'",".$value1.",".time().")');
But this time I do not have variable $user_id. I only have the username. So I need to do one more query to USERS dataase to get user_id from row where username is equal to my variable.
Is it possible not to do 2 queries? I meen INSERT and SELECT in one query?
P.S. I know that mysql_query function is depreciated. I used it here jusst for example.
Try nested subquery:
INSERT INTO `TABLE1`
(`user_id`,`value1`,`date`)
VALUES
(
(
SELECT
`user_id`
FROM
`user`
WHERE
`username` = 'your-user-name' // should be unique in order to work
),
'value1',
NOW()
);
The following query may be what you're looking for but it may be better to first get the user_id and then proceed the insert:
INSERT INTO TABLE1 (user_id, value1, date)
SELECT MAX(user_id) + 1 -- If the user_id is an numeric value...
,'user-name-here'
,NOW()
FROM TABLE1
With this query i'm getting the last user_id from the table and i increment it by 1 in order to insert the new user in the table.
Hope this will help you.
This question already has answers here:
Which DB design is faster: a unique index and INSERT IGNORE, or using SELECT to find existing records?
(6 answers)
Closed 9 years ago.
mysql database.
Table have index on field "Code".
I need to insert to table new rows.
What works faster?
1)
simple index on field Code - for fast select
befor insert check rows : SELECT COUNT(*) FROM table WHERE Code = 'NewCode';
simple insert(if rows not found): Insert into table values ('NewCode')
2)
unique index on field Code - for insert
Insert IGNORE into table values ('NewCode')
For me more secure (and can be a backup of changes - better way) is the first, but I think that the action is similar.
Consult http://dev.mysql.com/doc/refman/5.5/en/insert.html for more detali
paladinux
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
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.