MySql insert last id after each insertion - php

i'm trying to insert some rows from one table to another and add last inserted id to first table:
INSERT INTO tableA (fooA, fooA2) SELECT fooB, fooB2 FROM tableB;
And now i want to add last inserted id into tableB for each row
UPDATE tableB set tableA_id = LAST_INSERT_ID();
But for multiple records it's wrong. Any idea how to update tableB after each insertion into tableA? Is it possible to do with MySql query, or just write some PHP script?

Try to get it done with trigger .

Related

how to move sql table entry to different table

there is my code sample that deletes data from queue_db,I need code which inserts queue_db values to staff_db table.enter image description here
You can use SQL query
Copy all record one table to another
insert into staff_db select * from queue_db
This will transfer the data one table to another, later you can delete records conditionally from first table
If the columns names are different then
Copy all record one table to another with column name alias
insert into staff_tb(column1, column2, column3) select c as column1, b as column2, d as column3 from queue_db
where c,b,d are the columns in the second table
If you want to filter it with condition then add where at the end of the query with <tablename.columnname> = <value>
Copy all record one table to another without duplicates
To prevent duplicates, you can use this answer Prevent Duplicate I don't want repeat the same answer here
If you want copy those data into fresh new table like doing a backup then
Copy all record one table to a fresh new table
select * into newtable from queue_db
Then you run delete query for table which you copied from
Delete all records from a table
delete from queue_db

PHP add first table column id to another table column id

There are 2 Table in the Database.
tbl_second_category
tbl_third_category
I'm already passing
col_first_category_id
from
table1 tbl_second_category
to
Table2 tbl_third_category col_second_category_id
using select box control
You can see id 13 in both the Tables
when I'm passing above id, at the same time how can I pass col_second_category_id from Table1
tbl_second_category to column col_for_delete_row Table2 tbl_third_category.
Thank you
then use trigger, you may need more then one for update and delete events... for example:
CREATE TRIGGER trigger_name AFTER INSERT ON tbl_second_category
FOR EACH ROW
UPDATE tbl_third_category
SET col_for_delete_row = NEW.col_second_category_id
WHERE col_second_category_id = NEW.col_first_category_id;

Copying tables mysql

I am newbie to sql advance and i am trying to insert rows to one table by selecting from another one. But some bug is coming out.
Here is my problem
So i have two tables
table1 having id(autoincrement), names
table2 having id(autoincrement), names
Now at starting table1 is empty and table2 having 2 rows
1,'myself'
2,'yourself'
So the problem starts here
When i execute following query
Insert into table1 (names) select (names) from table2
So now both rows of table2 must be copied to table1
Ya its working fine.
But what about autoincrement value of id?
By till now table1 autoincrement id should store 3 since next row to be inserted should have id 3
But its not working like expected so table1 autoincrement id stores 4 i.e, 1(current id value)+2*(no of rows inserted)-1
So next time when i execute same query it inserts row with id 4. Skips id=3.
This is problem hope you all got what i am talking about.
Thanks for helping in advance.
Try this
INSERT INTO TABLE1 SELECT * FROM TABLE2
Go ahead an copy your data over, and then you can modify the starting auto_inc to whatever you choose.
ALTER TABLE table AUTO_INCREMENT = 1000;
Alter Table will take around 1 sec as it first creates shadow of the table then the query is processed and if the no. of rows of table goes above then it may take around 20-30 sec. so I dont think that alter is good answer for this problem. Hope someone will help you with insert command such as:
Insert into table1 set id = 1; etc...

Insert distinct records in the table while updating the remaining columns

This is actually a form to update the team members who work for a specific client, When i deselect a member then it's status turns to 0.
I have a table with all unique records. table consists of four columns -
first column is `id` which is unique and auto_incremented.
second column is `client_id`.
third column is `member_id`. (these second and third columns together make the primary key.)
fourth column is `current` which shows the status (default is 1.).
Now i have a form which sends the values of client_id and member_id. But this forms also contains the values that are already in the table BUT NOT ALL.
I need a query which
(i) `INSERT` the values that are not already in the table,
(ii) `UPDATE` the `current` column to value `0` which are in the table but not in the form values.
here is a screenshot of my form.
If (select count(*) from yourtable where client_id = and member_id = ) > 0 THEN
update yourtable set current = 0;
ELSE
insert into yourtable (client_id,member_id,current) values (value1,value2,value3)
First of all check if the value exists in the table or not, by using a SELECT query.
Then check if the result haven't save value so it will be inserted, else show an error .
This would be a great time to create a database stored procedure that flows something like...
select user
if exists update row
else insert new row
stored procedures don't improve transaction times, but they are a great addition to any piece of software.
If this doesn't solve your problem then a database trigger might help out.
Doing a little research on this matter might open up some great ideas!
Add below logic in your SP
If (select count(*) from yourtable where client_id = <value> and member_id = <value>) > 0 THEN
update yourtable set current = 0;
ELSE
insert into yourtable (client_id,member_id,current) values (value1,value2,value3)
if you want simple solution then follow this:
*) use select with each entry in selected team.
if select returns a row
then use update sql
else
use insert sql.
In your case member_id & client_id together makes the primary key.
So , you can use sql ON DUPLICATE KEY UPDATE Syntax.
Example:
$sql="INSERT INTO table_name SET
client_id='".$clientId."',
member_id='".$member_id."',
current='".$current."'
ON DUPLICATE KEY
UPDATE
current = '".$current."'
";
In this case when member_id & client_id combination repeats , it will automatically executes update query for that particular row.

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

Categories