How to join two tables with one similar column into one table - mysql

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

Related

Subtracting Values from Different Tables

I have two tables:
Table A
ID Number Profile
1 100 Baker
2 75 Fields
3 100 Wayward
Table B
ID Number Tag Sender
1 50 on chris
2 50 off james
What I am try to do subtract the values from Table A and Table B who has the same ID numbers
Table C
ID Number
1 50
2 25
SELECT A.ID,A.NUMBER-B.NUMBER DIFF
FROM TABLE_A AS A
JOIN TABLE_B AS B ON A.ID=B.ID

How to insert rows for each of the TYPE column?

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.

Generating SQL query with given number of rows in a query

I need to turn my table to this given output table using the column order table.
Is this even possible using SQL only?
I looked up the answer on the internet and I've seen people generally use PIVOT. However, my table has as many columns as there are rows in the column order row. So is this possible?
Any ideas/hints on how to do this are greatly appreciated.
Table I already have (yes, it's sorted by A)
A B C
---------- ---------- ----------------------
1 1 74
1 2 95
2 1 78
2 2 10
2 3 33
Order of Columns (another table I have)
B
-----
2
1
3
Wanted table (sorted by A)
A COLUMN 2 COLUMN 1 COLUMN 3
--------------------------------
1 95 74 0
2 10 78 33
As others pointed out, you need a column in the Order of Columns table representing the order. Assume that column is id, then you can use query:
select t.A,
ifnull(max(if (o.id=1, t.c, null)),0),
ifnull(max(if (o.id=2, t.c, null)),0),
ifnull(max(if (o.id=3, t.c, null)),0)
from t
join t_order o on o.b=t.b
group by t.A
See db-fiddle

Find duplicates based on two fields and delete them

I have a table called tourn_results there are multiple records that share the same tournamentId.
Example of a tourn_result record set
uniqueId tournamentId playerRank
1 111 1
2 111 2
3 111 3
4 222 1
5 222 2
6 222 3
7 333 1
8 333 2
9 333 3
10 333 4
11 111 1
12 111 2
13 111 3
For each tournament there cannot be more than 1 playerRank = 1
(Only one first place per tournamentId)
I need to to find each duplicate and delete them, can't find a suitable answer. I appreciate your help.
This will delete everything but the rows with the lowest uniqueid for each combination of (tournamentId,playerRank).
delete from tourn_results x
where uniqueid <> ( select min(y.uniqueid)
from tourn_results y
where y.tournamentid = x.tournamentid
and y.playerrank = x.playerrank );
You can add a unique index on the columns you don't want to have duplicates. It will drop all your duplicates.
ALTER IGNORE TABLE tourn_result ADD UNIQUE INDEX idx_name (tournamentId, playerRank);
The other option is to create a new temp table, insert your duplicates rows and delete them from your actual table.
In MySQL, this is most easily done with a left join:
delete tr
from tourn_results tr left join
(select tr2.tournamentid, min(uniqueid) as minui
from tourn_results tr2
where tr2.playerrank = 1
group by tr2.tournamentid
) ttr
on ttr.minui = tr.minui and ttr.tournamentid = tr.tournamentid
where tr.playerrank = 1 and ttr.minui is null;
I'm not so sure that deleting the additional "ties" for first place is the right thing to do, however.

MySQL and gathering and counting information from multiple tables

I am having issues figuring out how to do this. This isnt the real problem, but something very similar.
I have table A
ID Name
10 Bob
11 Tom
12 Suzie
13 Billy
14 Rob
15 Ben
Then table B, where B_ID references to ID in table A
B_ID Value
11 1500
13 2600
Then Table C where C_ID references to ID in table A
C_ID MatchedWith
10 11
12 13
14 11
15 11
The intention of this query is to list the Names of the people in table B, and how many people that are matched with them from C
...So the resulting query would give something like:
Name Count
Tom 3
Bily 1
I am completely boggled on how to do this, so any help would be super! thank you!
SELECT
A.Name,
COUNT(*) as 'Count'
FROM
C
JOIN B
ON C.MatchedWith = B.B_ID
JOIN A
ON A.ID = B.B_ID
GROUP BY A.Name
ORDER BY Count DESC;