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
Related
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.
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);
Let's assume that I have a table 'A' which holds the client's orders - so it is updated within every second - and I want to copy every record of it and put it in a new table 'B'.
How would you manage to do that knowing that every moment new records are added/updated and you cant just simply 'copy + paste' it (cause in that moment new records will be added/updated) ? Do you know any way to do that ?
Please try:
CREATE TRIGGER trigger_INSERTItemDetails ON TABLEA
FOR INSERT AS
BEGIN
INSERT INTO
tableB
(
colB1,
colB2,
colB3
)
SELECT
colA1,
colA2,
colA3
FROM
INSERTED
END
and make sure that you are inserting a NOT NULL value to column Primary column of table.
Similarly you created for UPDATE and DELETE actions, so you don't need to copy separately because all actions which happen in TableA it will affect TableB also.
I created a table w_provider_remove to keep the Provider_no and max(prov_effective_Date) of the records in w_providers_load because I have duplicate providers but some have expired. I later try to join the w_providers_load table and the w_provider_Remove table and insert the records into the w_providers_main table. The problem is I get back to many records because it turns out I have more than one record for the particular provider with the same effective date. How can I limit it so it only inserts one of them? Or maybe there is another way to go about this were I do not need 3 tables to accomplish this task
Truncate w_provider_remove;
insert into w_provider_remove
select provider_no as provider_no, max(PROV_DATE_EFFECTIVE) as prov_date_effective
from w_provider_load
group by provider_no;
Truncate w_provider_main;
INSERT INTO w_provider_main
Select l.*
from w_provider_load as l
inner JOIN w_provider_remove as r on l.provider_no = r.provider_no AND l.prov_date_effective = r.prov_date_effective;
If you want to limit the rows in the table to one effective date per provider, then create a unique index:
create unique index w_provider_load_provider_effectivedate on w_provider_load(provider_no, prov_effective_date);
This will generate an error if two records for the same provider have the same effective date in the load.
You can do the same thing for the main table:
create unique index w_provider_main_provider_effectivedate on w_provider_main(provider_no, prov_effective_date);