I have a large database of products and everyday I wish to run a script that moves rows with active = '1' into a new table and deletes the original row.
But I can't seem to find the appropriate Mysql command to accomplish the migration.
It would be great if someone could shed some light on the situation.
Thanks a lot
You should be able to complete this with the following
CREATE TABLE NewTable LIKE OldTable;
INSERT INTO NewTable
SELECT * FROM OldTable WHERE Active = 1;
DELETE FROM OldTable WHERE Active = 1;
Just as curiosity, what is the point of doing this? If you're worried about the fuzzy rows you could do the delete as follows
DELETE OldTab FROM OlTable AS OldTab
INNER JOIN NewTable AS NewTab
ON OldTab.ID = NewTab.ID
without any details, here is what you will do:
create an INSERT statement to the new table AS SELECT from the old table WHERE active = 1.
create a DELETE statement that deletes from the first table any rows it finds in the second table.
commit;
Related
I know I am supposed to create a temporary table based on an existing row, remove the id and then insert an new row in my "real" table based on the temporary table. But it does not work in my prestashop.
$id = 4;
$createTemp = Db::getInstance()->Execute('CREATE TABLE temp_table AS SELECT * FROM my_table WHERE id="'.$id.'"');
$updateTemp = Db::getInstance()->Execute('UPDATE temp_table SET id=NULL');
$insertQuery = Db::getInstance()->Execute('INSERT INTO my_table SELECT * FROM temp_table');
$deleteTemp = Db::getInstance()->Execute('DROP TABLE temp_table');
If I make a var_dump of those I always get a FALSE. I tried to make the select only in a query and it does work so my SELECT * FROM my_table WHERE id="'.$id.'" is correct.
I need to be able to duplicate a row and I could'nt find another/a better way to do so. What do I have to change in my code to make it work ?
If source data is OK your code should work,
I would enable Prestashop mode_dev so you can see the query errors on-screen for better debug.
Since you're working with Prestashop - maybe your "my_table" id is not called id but something different like "id_product" ?
If this is the case, first create will fail and all other queries will fail consequently.
I am trying to delete a row in i.e. table 2 where the id is copied from table 1 query. I want to delete this row from table 2 and also from table 1 but I am having an issue where the command I have used all work but it does not seem delete from the tables. I believe this might be because of the relationship the tables have(I used mysql workbench to make the DB design)
I used this command :
delete
from doctorsTable
where Users_idUser in (select Users_idUser from Users where idUser = 20)
This is the relation :
As mentioned, I am trying to delete the row from doctorsTable with Users_idUser=20 and automatically it would delete from Users table idUser also with 20. I have tried the above command, it seems to run but its not really deleting the rows . please help !
I think what you want to do is to delete all rows with idUser from both tables with one statement.
You can do so by joining and deleting them like:
DELETE doctorsTable, Users
FROM doctorsTable
INNER JOIN Users ON doctorsTable.Users_idUser = Users.idUser
WHERE doctorsTable.Users_idUser = 20
In the DELETE line, you specify the table(s) from which to delete matching rows.
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...
I have a PHP script that:
Copies new rows from the table "new" to "active" and delete the existing ones in "new".
Update the existing data and delete the ones in "new", if there is already a row with the same id_measurement in "active"
My current solution uses Laravel Eloquent. The problem is that the MySQL CPU usage is very high (90-100% on Mac) with over 10000 rows each time. Is there a faster way to do this? Maybe just with SQL?
Edit:
Everything is working fine now, expect the update part:
UPDATE foo_new as new
JOIN foo_active as active
ON active.id_bar = new.id_bar
SET active.blah=new.blah,
active.time_left=new.time_left
It's still really slow and uses a lot of the CPU.
Edit 2:
The solution are indexes. :)
Why do you bring it on application layer? delete and update the table using the power of mysql
I have done something similar use it
1) Update the table1 with new values of table2 using join
UPDATE tab1
INNER JOIN tab2 ON (tab1.DnameId = tab2.DNDOMAIN_ID)
SET
tab1.col = tab2.col;
2) For deletion - delete all the rows from table1 which are not in table2
delete from tab1 where tab1 .id not in (
select id from tab2
)
I run points system on my site so I need to keep logs of different action of my users into database. The problem is that I have too many users and keeping all the records permanently may cause server overload... I there a way to keep only 10 records per user and automatically delete older entries? Does mysql have some function for this?
Thanks in advance
You can add a trigger that takes care of removing old entries.
For instance,
DELIMITER //
CREATE definer='root'#'localhost' TRIGGER afterMytableInsert AFTER INSERT ON MyTable
FOR EACH ROW
BEGIN
DELETE FROM MyTable WHERE user_id = NEW.user_id AND id NOT IN
(SELECT id FROM MyTable WHERE user_id = NEW.user_id ORDER BY action_time DESC LIMIT 10);
END//
Just run an hourly cron job that deletes the 11th - n records.
Before insert a record you could check how many the user has first. If they have >=10 delete the oldest one. Then insert the new one.
If your goal is to have the database ensure that for a given table there are never more than N rows per a given subkey (user) then the correct way to solve this will be either:
Use stored procedures to manage inserts in the table.
Use a trigger to delete older rows after an insert.
If you're already using stored procedures for data access, then modifying the insert procedure would make the most sense, otherwise a trigger will be the easiest solution.
Alternately if your goal is to periodically remove old data, then using a cron job to start a stored procedure to prune old data would make the most sense.
When you are inserting a new record for a user. Just do a query like this before (Don't forget the where-condition):
DELETE FROM tablename WHERE userID = 'currentUserId' LIMIT 9, 999999
After that you can insert new data. This keeps the data always to ten records for each user.
INSERT INTO tablename VALUES(....)
DELETE FROM Table WHERE ID NOT IN (SELECT TOP 10 ID FROM Table WHERE USER_ID = 1) AND USER_ID = 1
Clearer Version
DELETE FROM Table
WHERE ID NOT IN
(
SELECT TOP 10 ID FROM Table WHERE USER_ID = 1
)
AND USER_ID = 1