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.
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
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);
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
Couldn't find exactly what I'm looking for anywhere else.
So I have table 1
users
----------
id
username
password
bio
isuser
email
and table 2
wp_users
----------
id
user_login
nice_username
password
email
There are 500 rows in 'wp_users' table. I would like to copy 'id' and 'user_login' into the users table ('id' and 'username') for each row.
How can I do this? MySQL isn't my strong point lol.
UPDATE: I have updated the tables above as I tried to simplify it but in return got the wrong solution.
You can use an INSERT - SELECT statement:
INSERT INTO `users` (id,username,password,bio,isuser,email) SELECT id,
user_login,null,null,null,null FROM `wp_users`;
You can put another fields or even static data on each field of the SELECT part. I've put nulls just to illustrate.
Just remember that anything the SELECT fetches will be inserted in the table the INSERT statement says (so you can use clauses like WHERE, GROUP BY, HAVING, etc on the SELECT part).
I believe you are looking for a simple insert statement:
INSERT INTO users (id,username)
SELECT W.id,
W.user_login
FROM wp_users W
WHERE NOT EXISTS (SELECT NULL
FROM users U
WHERE U.ID = W.ID);
I am assuming the other columns in your users table are nullable, if not, you can add more columns to the select statement with the values in you'd like for the other columns.
You haven't mentioned any other keys or constraints, so I have assumed there aren't any.
I have also provided a check in the WHERE clause to see if a row already exists for the same ID, so you only insert a row for each ID once.