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

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

Related

How to conditionally set value for mysql row group? [duplicate]

I want to generate a new column in my table that is true if a row exists with certain conditions.
name | col1 | col2 | flag
--------------------------
a 1 2 0
a 2 3 0
b 1 2 0
b 4 3 0
Lets say I want to set the flag to 1 for every name identifier if a row exists with that name and where col1=2 and col2 = 3. So this would result in:
name | col1 | col2 | flag
--------------------------
a 1 2 1
a 2 3 1
b 1 2 0
b 4 3 0
because for a a row with col1=2 and col2 = 3 exists, but for b, such a row doesn't exist.
In pseudocode I want something like this:
ALTER TABLE table_name
ADD flag TINYINT(1)
IF ##row with condition col1=value1 and col2=value2 exists#
GROUP BY name
How can I generate this column?
So you want just to get those values from db? or you want to add column? those are 2 different goals.
So if you need just to get those values you can:
http://sqlfiddle.com/#!9/65b4c2/1
SELECT t.*, t2.flag
FROM table_name t
LEFT JOIN (
SELECT name, MAX(IF(col1=2 AND col2=3,1,0)) flag
FROM table_name
GROUP BY name
) t2
ON t.name = t2.name
and if you really need to add new column then you go this way:
http://sqlfiddle.com/#!9/226fb3/1
ALTER TABLE table_name ADD COLUMN flag TINYINT;
UPDATE table_name t
LEFT JOIN (
SELECT name, MAX(IF(col1=2 AND col2=3,1,0)) flag
FROM table_name
GROUP BY name
) t2
ON t.name = t2.name
SET t.flag=t2.flag

How to set MYSQL column to certain value if a certain row exists?

I want to generate a new column in my table that is true if a row exists with certain conditions.
name | col1 | col2 | flag
--------------------------
a 1 2 0
a 2 3 0
b 1 2 0
b 4 3 0
Lets say I want to set the flag to 1 for every name identifier if a row exists with that name and where col1=2 and col2 = 3. So this would result in:
name | col1 | col2 | flag
--------------------------
a 1 2 1
a 2 3 1
b 1 2 0
b 4 3 0
because for a a row with col1=2 and col2 = 3 exists, but for b, such a row doesn't exist.
In pseudocode I want something like this:
ALTER TABLE table_name
ADD flag TINYINT(1)
IF ##row with condition col1=value1 and col2=value2 exists#
GROUP BY name
How can I generate this column?
So you want just to get those values from db? or you want to add column? those are 2 different goals.
So if you need just to get those values you can:
http://sqlfiddle.com/#!9/65b4c2/1
SELECT t.*, t2.flag
FROM table_name t
LEFT JOIN (
SELECT name, MAX(IF(col1=2 AND col2=3,1,0)) flag
FROM table_name
GROUP BY name
) t2
ON t.name = t2.name
and if you really need to add new column then you go this way:
http://sqlfiddle.com/#!9/226fb3/1
ALTER TABLE table_name ADD COLUMN flag TINYINT;
UPDATE table_name t
LEFT JOIN (
SELECT name, MAX(IF(col1=2 AND col2=3,1,0)) flag
FROM table_name
GROUP BY name
) t2
ON t.name = t2.name
SET t.flag=t2.flag

Compare two tables and Insert/Update data

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

Update a column of a table with the count of other column in the same table in a MySQL server

I have a table like this:
recordid customerid product id count
1 2 12 3
2 4 10 1
3 2 3 3
4 3 12 2
5 3 10 2
6 2 7 3
7 5 3 1
8 ....
9 ....
I want an update query that will count the no of occurrence of each customer id and update the count column which will initially be empty.
the end result should be like above
The column names are dummy, my actual table is different.
It has data in millions of rows.The query should be speedy
I tried the query but it gets stuck...
update tablename, (select count(recordid) as count,customerid from tablename group by customerid) as temp set count=temp.count where customerid=temp.customerid
You can use JOIN in UPDATE.
Try this:
UPDATE TableName A
JOIN
(SELECT customerid,Count(customerid) as cnt
FROM TableName
GROUP BY customerid) as B ON A.customerid= B.customerid
SET A.count = B.cnt
This doesn’t see right:
update tablename, (select count(recordid) as count,customerid from tablename group by customerid) as temp set count=temp.count where customerid=temp.customerid
Why is there a comma after update tablename like this:
update tablename,
I am also reformatting for readability:
UPDATE tablename (
SELECT count(recordid) AS count, customerid
FROM tablename GROUP BY customerid
) as temp
SET count=temp.count
WHERE customerid = temp.customerid

MySQL Delete duplicates in consecutive rows

Suppose this table:
ID ColA ColB
1 7 8
2 7 9
3 7 9
4 5 8
5 6 9
6 6 9
7 5 4
The PK is the ID coumn.
Now, I want to delete all duplicates of ColA and ColB in consecutive rows.
In this example rows 2,3 and 5,6 contain duplicates.
These shall be removed so that the higher ID is remained.
The output should be:
ID ColA ColB
1 7 8
3 7 9
4 5 8
6 6 9
7 5 4
How can this be done with mySQL?
Thanks,
Juergen
SELECT
ID
FROM
MyTable m1
WHERE
0 < (SELECT
COUNT(*)
FROM
MyTable m2
WHERE
m2.ID = m1.ID - 1 AND
m2.ColA = m1.ColA AND
m2.ColB = m1.ColB)
and then you can use a
delete from MyTable where ID in ...
query. This way it would surely work in any version.
CREATE TEMPORARY TABLE duplicates (id int primary key)
INSERT INTO duplicates (id)
SELECT t1.id
FROM table t1
join table t2 on t2.id = t1.id + 1
WHERE t1.ColA = t2.ColA
and t1.ColB = t2.ColB
-- SELECT * FROM duplicates --> are you happy with that? => delete
DELETE table
FROM table
join duplicates on table.id = duplicates.id
Depending on how many records you have, this might not be the most efficient:
SELECT (SELECT TOP 1 id FROM table WHERE colA = m.colA AND colB = m.colB ORDER BY id DESC) AS id, m.*
FROM (SELECT DISTINCT colA, colB
FROM table) m
There might be syntax errors because I usually use mssql, but the idea should be similar.
I've called the first table 'test'.
Firstly create a table that will hold all the identical combinations of ColA and ColB:
create temporary table tmpTable (ColA int, ColB int);
insert into tmpTable select ColA,ColB from test group by ColA, ColB;
Now, select the maximum id in the original table for each identical combination of ColA and ColB. Put this into a new table (called idsToKeep because these are the rows we do not want to delete):
create temporary table idsToKeep (ID int);
insert into idsToKeep select (select max(ID) from test where test.ColA=tmpTable.ColA and test.ColB=tmpTable.ColB) from tmpTable;
Finally, delete all the entries from the original table that are not in the idsToKeep table:
delete from test where ID <> all (select ID from idsToKeep);