Compare two tables and Insert/Update data - sql-server-2008

I have two tables as below what i need to do is
Loop through Table2 rows and need to check if table1ID in Table2 exists in table1. If exists update existing table1 row active flag to 0 and insert a new record in table1 with values from Table2 row. If does not exist insert a new record in table1 with values from Table2 row. We can assume E1, E2,E3 columns correspond to Q1A, Q2A, Q3A in table1.
Table1:
Table1ID Q1A Q2A Q3A Active
1 2 Test 1 1
2 3 Test2 1 1
3 4 Test3 1 1
4 5 Test4 1 1
Table2:
Table1ID E1 E2 E3
1 2 TestData1 1
2 3 TestData2 1
3 4 TestData3 1
5 5 TestData5 1
6 7 TestData6 0

If exists
update existing table1 row active flag to 0
and
insert a new record in table1 with values from Table2 row.
If does not exist insert a new record in table1 with values from Table2 row
From what you wrote, it seems you want to update the Active column to 0 for an existing record and insert the records from Table2 in any case.
Below should work for you:
MERGE Table1 t1
USING Table2 t2
ON t1.Table1ID = t2.Table1ID
WHEN MATCHED THEN
UPDATE
SET t1.Active = 0;
INSERT (Table1ID, Q1A, Q2A, Q3A, Active)
VALUES (t2.Table1ID, E1, E2, E3, 1);

So in both cases, if the record exists, you're going to insert a row from Table2. The only difference is, if it exists in Table1, you want to set it to zero.
DECLARE #currid INT, #maxid INT, #lastid INT
SELECT #currid = 0
, #lastid = 0
, #maxid = MAX(Table1ID)
FROM Table2
WHILE (#currid < #maxid)
BEGIN
-- Get the next minimum Table1ID from Table2
SELECT #currid = MIN(Table1ID)
FROM Table2
WHERE Table1ID > #lastid
-- See if it exists in Table1
IF EXISTS (SELECT 1 FROM Table1 WHERE Table1ID = #currid)
BEGIN
-- Set Active to zero as specified
UPDATE Table1ID
SET Active = 0
WHERE Table1ID = #currid
END
-- Copy row from Table2 - I assume with all columns as well as 1 for Active
INSERT INTO Table1
SELECT *, 1
FROM Table2
WHERE Table1ID = #currid
-- Set our ID value for the next loop
SET #lastid = #currid
END

Related

MySQL- Update column with same id from different table by combining its values with same id

TableA
id
number
1
0
1
1
2
0
3
0
3
1
3
2
TableB
id
number2
1
2
3
I want to trigger update the TableB whenever TableA is updated.
Result should be
TableB
id
number2
1
0,1
2
0
3
0,1,2
The following statement display the result
select `id`,
group_concat(`number` separator ',')
from TableA
group by `id`
but I want to trigger update the TableB whenever TableA is updated.
(Optional): Only update the id which is updated will be preferred.
New workaround
CREATE TRIGGER `store_jan` AFTER UPDATE ON `TableA` FOR EACH ROW BEGIN
UPDATE `TableB`
SET number2 = group_concat(`number` separator ',') from TableA group by `id`
WHERE `TableB`.`id` = `TableA`.`id`
END
But showing error
Please help

MySQL - 1 Master List Table Not IN Table 2 but IN Table 3

Can you kindly help me with my query?
In my database, I have 3 tables,
Table 1 - Student Master List (9 Records)
Table 2 - AM (4 Records)
Table 3 - PM (3 Records)
Table 2 and table 3 have the same structure but Table 2 is more priority than Table 3, anyway
I want to see the records from Table 1 which are NOT IN Table 2 BUT there's a record IN Table 3. Table 2 (4) + Table 3 (3) = 7 Records
But how can I show the 2 records from the master list
sample database
My query is something like this:
select * from table1 t1
where (id, lname, fname, mname) NOT IN
(select id, lname, fname, mname from table2) and
(id, lname, fname, mname) IN
(select id, lname, fname, mname from table3)
But when I did this, It just shows some records from table 2 and table 3
If you have a common key among all tables which is (id, lname, fname, mname) below will work. If your common key is somewhat different, adjust WHERE clauses in both subqueries to only include the common key (column(s)).
Use EXISTS to include records present in table 3 and NOT EXISTS to exclude records present in table 2:
select *
from table1 t1
where
not exists (
select 1
from table2 t2
where t1.id = t2.id and t1.lname = t2.lname and t1.fname = t2.fname and t1.mname = t2.mname)
and exists (
select 1
from table3 t3
where t1.id = t3.id and t1.lname = t3.lname and t1.fname = t3.fname and t1.mname = t3.mname)
I suspect you just need to union table 2 and 3 and left join table 1 testing for null values
drop table if exists t1,t2,t3;
create table t1 (id int);
create table t2 (id int);
create table t3 (id int);
insert into t1 values (1),(2),(3),(4),(5),(6),(7),(8),(9);
insert into t2 values (2),(3),(4),(5);
insert into t3 values (1),(6),(7);
select t1.*
from t1
left join
(select id from t2
union all
select id from t3)s on s.id = t1.id
where s.id is null;
+------+
| id |
+------+
| 8 |
| 9 |
+------+
2 rows in set (0.00 sec)

MYSQL: Help for fast update

i need some help to get a fast update on my table in MySQL
Table 1
id | value
1 0
2 0
3 0 ...
Table 2
t1_id | t2_id
1 2
1 3
3 5 ...
Have about 150,000 rows in table 1, and about 1,3 million in table 2. I need set t1.value = 1 when t1.id exists in table 2.
update table1 t1, table2 t2
set value = 1
where t1.id = t2.id;
Without some distinct parameter, it will do many times for each id, making it slow to update all t1 rows.
Any help would be gladly accepted.
what about:
UPDATE t1
SET t1.value = 1
FROM table_t1 t1
WHERE EXISTS (SELECT 1
FROM table_t2 t2
WHERE t2.id = t1.id
)
what about:
update table1
set value=1
from table2
where table1.id=table2.t1_id

Mysql: Add a column and fill in

I have a table:
Table1:
Id value
1 10
2 20
3 30
4 40
In table 2:
Id
1
1
2
2
3
4
4
4
I want to get the results:
Table2:
Id value
1 10
1 10
2 20
2 20
3 30
4 40
4 40
4 40
I know add a column with following code:
ALTER table Table2 ADD value int(11)
How to fill the data??
I think these SQL useful to you.
UPDATE Table2 LEFT JOIN Table1 ON Table2.id = Table1.id
SET Table2.value = Table1.value
Thank you.
you need to first ALTER the table and then fill the values using JOIN command as:
ALTER table Table2 ADD value int(11);
Then insert / fill the values as:
UPDATE Table2 INNER JOIN Table1
ON Table2.id = Table1.id
SET Table2.value = Table1.value
You could run this update command juste after tours.
UPDATE Table2 SET value = (SELECT value FROM Table1 WHERE Table2.Id = Table1.Id)

Update table2 after update table1 in MySQL using Trigger

I'm tryin to do a trigger with mysql to update a table2 after update a table1, basically the logic of the operation is this one:
i have a table1 which have next fields:
idtable1
idtable2
units
next table is like this
idtable2
totalunits
so i need to update totalunits field of table2 with the sum of all units of table1 with same id of table2, by example:
lets say that table1 has these values:
idtable1 | idtable2 | units
___________________________
1 | 1 | 3
2 | 1 | 2
3 | 2 | 2
then my table2 should be
idtable2 | totalunits
________________________
1 | 5
2 | 2
i know maybe it's super easy but i can't find any solution, i already created a trigger but it's not working
update `table2`,`table1` set totalunits= sum(table1.units) where table2.idtable2=table1.idtable2
You can use below query:
UPDATE table2 t2 SET t2.`counts` = (SELECT SUM(t1 .`units`) FROM table1 t1 WHERE t1.`id2` = t2.`id2`);
With a trigger
DELIMITER $$
CREATE TRIGGER upd_units AFTER UPDATE ON table1
FOR EACH ROW
BEGIN
SET #diff := OLD.units - NEW.units;
UPDATE table2 SET total_units = total_units - #diff WHERE idtable2 = OLD.idtable2;
END;$$
DELIMITER ;