I have a table A with columns c1,c2,c3 and c4. I have another table B with the same columns(c1,c2,c3,c4).
The contents of both the tables are almost same. The only difference is that the table A contains some duplicate rows since no unique index in A.
I have a view C joining A and B. How can I find the rows that are updated/deleted in A, so that the same can be updated/deleted from B as well, using view C.
Here is sample data.
A
c1 c2 c3 c4
1 2 3 4
1 2 3 4
1 3 3 4
2 5 6 7
B
c1 c2 c3 c4
1 2 3 4
1 3 3 4
2 5 6 7
So, B= A - (duplicate entries)
In B, (c1,c2) is the unique key.
I have a view C with joins to A and B.
Goal : If a row is deleted from A, say (1,3,3,4), the same row should be deleted from B also.
Question : How can I use the view C to achieve the above mentioned Goal
Related
How to insert rows for each of the type column? If there is two type, type 1 and type 2, then I need to insert two rows and also need to change the order and id value for whole table.
current status:
CHOICE Table
id choice type order
1 AA 1 1
2 BB 1 2
3 CC 1 3
4 AAA 2 4
5 BBB 2 5
6 CCC 2 6
7 DDD 2 7
Required updated table:
Now i wan to insert choice "000" for each type. The updated table will be look like bellow. How can I achieve this?
updated CHOICE Table
id choice type order
1 000 1 1
2 AA 1 2
3 BB 1 3
4 CC 1 4
5 000 2 5
6 AAA 2 6
7 BBB 2 7
8 CCC 2 8
9 DDD 2 9
here, id and order column serialized again.
The actual table is too big, so I cannot insert by edit. Please help for this complex query. I have no clue to solve this.
Use insert . . . select to insert the rows:
insert into choice (choice, type)
select distinct '000', type
from choice;
This assumes that id is automatically assigned (and it will be different from your example).
However, it looks like you want to update the order as well. For this, I would suggest an update:
update choice c join
(select c2.*,
row_number() over (partition by choice order by (order is null) desc, order) as new_order
from choice c2
) c2
on c.id = c2.id
set c.order = c2.new_order;
As an editorial comment, order is a very bad choice for a column name because it is a SQL keyword.
I want to synchronize the two columns (one table, two columns).
Example:
A-1 references B-2, 3, 4 but A-2 references only B-1.
How to add B-1 reference B-3 and B-4 with mysql query?
A B
-------
1 2
1 3
1 4
2 1
3 1
4 1
5 6
5 7
6 5
7 5
I think you want this:
insert into t(a, b)
select b, a
from t
where not exists (select 1 from t t2 where t2.a = t.b and t2.b = t.a);
This will put all pairs into the table, in both directions.
I need to SELECT from a db and don't show rows that are identical in SOME rows (or actually hide the rows that are identical to eachother except 1 item...)
Let's give an example:
ID C1 C2 C3
1 3 3 4
1 5 5 4
1 2 3 4
1 6 5 4
1 2 3 4
After SELECT i want:
ID C1 C2 C3
1 X 3 4
1 X 5 4
where "X" has no importance...i have to show that column but i don't care which one is shown.
Is this possible with a simple SELECT query?
To sum up, if i ask the question regarding this specific example, What can i do to SELECT from that table and show only one of the rows if it has identical duplicates in ID, C2 and C3?
SIDENOTE: this MYSQL: SELECT Method - but don't show duplicates / GROUP or DISTINCT? doesn't help.
Have you tried GROUP BY?
SELECT Id, MAX(C1), C2, C3
FROM SomeTable
GROUP BY Id, C2, C3
Since you don't care about C1, in this query, I get the largest C1 value.
I want to start by telling that this is a really specific question. It's also highly related to this thread. There they use:
SELECT foo,bar FROM my_table GROUP BY foo,bar
But what differentiates this question against the linked one is when a dupplicate is found; an arbitrary row of the duplicates is returned. I need to get the row with the highest value of a third column.
For example:
a b 20
a b 10
a a 2
b b 10
b b 29
c c 2
c c 4
c c 9
Desired output:
a b 20
b b 29
c c 9
I have 3 tables, A and B and C.
sample data for Table A
nid tid
101 3
101 4
101 7
103 3
103 5
104 2
104 4
104 7
sample data for Table B
tid name
2 ram
3 shyam
4 krishna
5 shiv
7 narad
What I want is, in a Third Table C
id nid labels
1 101 shyam, krishna, narad
2 103 shyam, shiv
3 104 ram, krishna, narad
I know how to do this with PHP, but is there any way to do this mysql alone?
Both tables (A and B) have thousands of records and don't have any unique column at the moment.
I tried GROUP_CONCAT but I could not construct desired output.
Edit 1 - I forgot to mention that Table C already has id and nid column inserted, while labels column is empty. So I need help in constructing some query which can update all records of Table C with labels mentioned as above.
Thanks. Regards,
This will insert the records on TableC. Since ID is an autogenerated column, you can omit this in the INSERT clause.
INSERT INTO TableC(Nid, Labels)
SELECT a.nid, GROUP_CONCAT(b.Name) Labels
FROM TableA a
INNER JOIN TableB b
ON a.tid = b.tid
GROUP BY a.nid