Long time lurker, first post(er?)
I need to delete some duplicate entries in my database based on attributes spread across multiple tables. This is the way I've done but, but I'm sure there is a better way (I'm by no means a SQL expert!). Any pointers would be great. (When I went to do this for a second time, it failed)
I'm first getting all the rows from a table, sorting by date, then picking the newest entry (dup1). Then I'm matching that list to another table based on a value (dup2). Then finally creating a list of rows based in their ID that appears in both tables (dup3). Then I want to delete from the main table where the ID is in the 3rd temp table.
First, I created a temp table:
create temporary table dup1
as
select * from
(SELECT hex(media_id) as asset_id, folder_id, name, ingest_date
FROM media
order by ingest_date DESC) as dup
group by name having count(name)>1 and count(hex(folder_id))>1
Then created a second temp table:
create temporary table dup2
as
SELECT hex(asset_id) as asset_id, value FROM datavalues where name_id = 103 group by value having count(value)>1;
as
SELECT hex(asset_id), value FROM datavalues where name_id = 103 group by value having count(value)>1;
Created a final temp table that merges the two previous temp tables
create temporary table dup3
as
select dup1.asset_id from dup1
join dup2 on dup2.asset_id = dup1.asset_id
Then deleted all assets from the media table that exist in dup3
DELETE FROM media where hex(media_id) in (SELECT * from dup3);
Related
Scenario
1. Two tables exist called 'score_log' and 'games_played_count'
2. Computing the "count" of rows from score_log that uniquely describe the number of games that have been played by a given member. Each row is unique based on a combination of playerid, teamid and leagueid
3. The 'games_played_count' table contains a unique row based on playerid, teamid and leagueid with a variable column 'games_played_total' which is designed to be updated automatically when an INSERT OR DELETE occurs on the score_log table.
Current Solution
a. Two triggers defined on 'score_log' table. One trigger executes a stored procedure (defined below) using AFTER INSERT condition. The other trigger defined executes the stored procedure using AFTER DELETE condition.
b. The stored procedure that achieves the objective is defined below:
CREATE TEMPORARY TABLE temp_table AS
SELECT playerid, leagueid, COUNT(*) AS games_played_count
FROM score_log
GROUP BY playerid, leagueid;
UPDATE games_played_by_league
INNER JOIN temp_table ON games_played_by_league.playerid = temp_table.playerid AND games_played_by_league.leagueid = temp_table.leagueid
SET games_played_by_league.games_played_count = temp_table.games_played_count
WHERE games_played_by_league.playerid = temp_table.playerid AND games_played_by_league.leagueid = temp_table.leagueid;
Is this the most optimal way to achieve the desired outcome?
I need to validate the last inserted row in a table every time using some constraints given by the user on a particular column in that table( Ex: age > 50...etc.).
I thought of using a temporary table to insert the row in both of my temporary table and my normal table. After then, I'll use query like Select * from tb_name USER_CONSTRAINTS [ Ex: select * from student where age>50 ] on temporary table. If the result is not null, then the last row satisfies the user constraint else it fails. After I'll delete the last added row from the temporary table and repeat the process for next row.
I don't know if this way is good or bad or is there some other efficient way to do this?
Edit:
The table has 5 columns
stud_id,
subject_id,
age,
dob,
marks
Here stud_id and subject_id act as foreign keys to two tables tbl_student and tbl_subject
I am trying to combine INSERT, UPDATE and WHERE NOT EXISTS in one and the same query.
What I have at the moment is these two queries that works as expected separately
INSERT INTO settings (mid) SELECT '123' FROM DUAL WHERE NOT EXISTS (SELECT mid FROM settings WHERE mid='123');
UPDATE settings SET vote = CONCAT_WS(',', vote, '22') WHERE mid = '123'
What I am trying to achieve is combine them together, so I can bother the db once
What I have is a table with two columns: mid that stores the unique user id, that column is also a primary, and another column that is called vote that stores the user votes in a comma separated order.
So, my aim here is to first check is the user is having a row already created for him (if not to create it) and then if the row exists to add the new vote 22 in my example to the list.
I have two tables, and i am trying to update the first table based on the value from second table. Table 1 contains the Sum of total weight and table two contains weight. when added up these values in table 2, it then shows total in table 1
For example if I update the foreign key in table 2 then the Amount in table should update. Please refer the screenshot attached.
My first table sql:
select LodingZoneID, Finaltotal from TransitList
My Second table sql:
SELECT `Suburb`, `LodingZoneID`, Total FROM `GenerateRun`
There is no reason for table 1. You can just run the query:
select fk, sum(val)
from table2
group by fk;
If you really need to store the sum in table 1, then you would be using triggers on table2 to handle insert/update/delete. This is rather cumbersome and it is generally better to do the calculation when needed, rather than trying to store the results in advance.
Or, You can create a View with desired output
-CREATE VIEW 'TransitList' AS
SELECT LodingZoneID, SUM(Total) FinalTotal FROM GenerateRun
GROUP BY LodingZoneID;
Whenever you want to access data, you can use
-SELECT LodingZoneID, FinalTotal FROM TransitList;
I have one table in which each row (event) has a unique (integer) ID. I have another table, which includes the same events, and I am trying to copy the ID column from the first table to the next with no luck. I have tried creating a blank column in the second table first and I have tried it without having a blank column prepared. When I run my queries, it will spin and say that it was successfully executed, but the column in the second table is never updated.
Here is my query:
insert into games2 game_id
(
select games.game_id as game_id
from
games2 join games
on games2.DATE = games.DATE and
games2.HOME = games.HOME and
games2.AWAY = games.AWAY
)
INSERT INTO Item (Name)
SELECT Item
FROM IName
I think this source would help you out Copy from one column to another (different tables same database) mysql