mysql join get info from 2 rows - mysql

i have 2 tables:
players
id | dni | name
-----------------
1 222 mike
2 333 gerard
3 444 mark
4 555 alfred
5 666 thomas
5 777 nicolas
teams
id | dni1 | dni2 | cat
------------------------
1 222 333 1
2 444 555 1
3 666 333 2
4 777 222 2
what i want is to make a select statement depending on the cat. so for
cat=1 should be:
mike gerard
mark alfred
and for cat=2 should be:
thomas gerard
nicolas mike
i tryed many join statements but i cant figure it out, can you help me please?
EDIT to make it more difficult, the players table have a new colum= points
players
id | dni | name | points
--------------------------
1 222 mike 1
2 333 gerard 2
3 444 mark 3
4 555 alfred 4
5 666 thomas 5
5 777 nicolas 6
now the result give us the sum of the points colums from both players and order by biggest sum number.
cat=1 should be:
mark alfred 7
mike gerard 3

select t.cat, p1.name name1, p2.name name2, p1.points + p2.points points
from teams AS t
join players AS p1 ON p1.dni = t.dni1
join players AS p2 on p2.dni = t.dni2
where cat = 1
order by points desc
DEMO

Related

I am trying to return combined rows in the same table based on a key in a second table

I probably haven't explained this very well in the title but I have two tables. Here is a simple version.
channel_data
entry_id channel_id first_name last_name model other_fields
1 4 John Smith
2 4 Jane Doe
3 4 Bill Evans
4 15 235
5 15 765
6 15 543
7 15 723
8 15 354
9 15 976
10 1 xxx
11 2 yyy
12 3 123
channel_titles
entry_id author_id channel_id
1 101 4
2 102 4
3 103 4
4 101 15
5 101 15
6 101 15
7 102 15
8 102 15
9 103 15
10 101 1
11 102 2
12 103 3
I am not able to re-model the data unfortunately.
I need to list all the rows with a channel_id 15 from channel_data and beside them the first_name and last_name which has the same author_id from channel_titles.
What I want to return is this:
Model First Name Last Name
---------------------------------
235 John Smith
765 John Smith
543 John Smith
723 Jane Doe
354 Jane Doe
976 Bill Evans
If Model was in one table and Names were in another this would be much simpler but I'm not sure how to go about this when they are in the same table.
========================================
Edited to clarify.
I need to get each model with a channel_id 15 from channel_data
For each model I need to look up the entry_id in channel_titles to find the author_id
I need to find the row with that author_id AND channel_id 4 in channel titles (each row with channel_id 4 has a unique author_id).
I need to take the entry_id of this row back to channel_data and get the first_name and last_name to go with the model.
I am well aware that the data is not structured well but that is what I have to work with. I am trying to accomplish a very small task in a much larger system, remodelling the data is not an option at this point.
I think sub-queries might be what I am looking for but this is not my area at all usually.
Ok, that is convoluted. However, based on your description, this query should give you the results you want. The WHERE and JOIN descriptions follow the logic you have described in your question.
SELECT cd1.model, cd2.first_name, cd2.last_name
FROM channel_data cd1
JOIN channel_titles ct1 ON ct1.entry_id = cd1.entry_id
JOIN channel_titles ct2 ON ct2.channel_id = 4 AND ct2.author_id = ct1.author_id
JOIN channel_data cd2 ON cd2.entry_id = ct2.entry_id
WHERE cd1.channel_id = 15
ORDER BY cd1.entry_id
Output:
model first_name last_name
235 John Smith
765 John Smith
543 John Smith
723 Jane Doe
354 Jane Doe
976 Bill Evans
Demo on SQLFiddle

Multi-event tournament standings

I am trying to grab a participants rankings in a multi-event tournament.
I can do a ranking for a single event pretty easily. Is there a way to find ALL in one go?
Given input: "Bob"
Data example: Desired output:
Name | Event | Score Name | Event | Score | Rank
-------------------- ----------------------------
Bob 1 100 Bob 1 100 1
Bob 2 75 Bob 2 75 3
Bob 3 80 Bob 3 80 2
Jill 2 90
Jill 3 60
Chris 1 70
Chris 2 50
Chris 3 100
Amy 1 85
Amy 2 95
Amy 3 65
The catch: I do not have access to the Rank()
function with my version of SQL, and updating is not possible in this scenario.
Clearly I could just do the score per event separately in a loop,
but I'd like to try to do it all in one go.
You can emulate a ranking function in MySQL using a self-join to values with a higher score in the same Event, and then counting the number of higher scores for each participant:
SELECT s1.Name, s1.Event, s1.Score, COUNT(s2.Name)+1 AS Rank
FROM scores s1
LEFT JOIN scores s2 ON s2.Event = s1.Event AND s2.Score > s1.Score
WHERE s1.Name = 'Bob'
GROUP BY s1.Name, s1.Event, s1.Score
ORDER BY s1.Name, s1.Event
Output:
Name Event Score Rank
Bob 1 100 1
Bob 2 75 3
Bob 3 80 2
Demo on dbfiddle

Why COUNT in the SQL HAVING condition doesn't calculate correct value?

When i solve this HackerRank problem
i found that my solution with condition
COUNT(total) = 1 doesn't work:
SELECT H.hacker_id AS hacker_id, H.name AS name, COUNT(C.challenge_id) AS total
FROM Hackers H
JOIN Challenges C ON H.hacker_id = C.hacker_id
GROUP BY H.hacker_id, H.name
HAVING
COUNT(total) = 1 OR -- HERE
OR
total = (
SELECT MAX(amount) FROM (
SELECT COUNT(challenge_id) AS amount FROM Challenges
GROUP BY hacker_id
) t
)
ORDER BY total DESC, hacker_id;
Output:
5120 Julia 50
18425 Anna 50
20023 Brian 50
33625 Jason 50
41805 Benjamin 50
52462 Nicholas 50
64036 Craig 50
69471 Michelle 50
77173 Mildred 50
94278 Dennis 50
96009 Russell 50
96716 Emily 50
2689 Joe 1
3432 Linda 1
5827 Kelly 1
6011 Robin 1
7537 David 1
7961 Michelle 1
9642 Joseph 1
9901 Lawrence 1
10975 Christine 1
11715 Louise 1
13075 John 1
13905 Evelyn 1
14307 David 1
14726 Emily 1
15109 Dorothy 1
17282 Norma 1
17311 Andrew 1
17663 Benjamin 1
17846 Alan 1
18709 James 1
19032 Andrew 1
19781 Jesse 1
19962 Patricia 1
20190 Aaron 1
21350 Bobby 1
21821 Carol 1
21916 Clarence 1
22396 Wayne 1
22455 Carolyn 1
23812 Jerry 1
24047 Elizabeth 1
25684 Alan 1
25990 Mildred 1
26802 Paul 1
27797 Helen 1
28766 Paul 1
29242 Jennifer 1
29841 Charles 1
30677 Keith 1
30778 Jose 1
30908 Helen 1
31770 Ashley 1
32364 Julia 1
32735 Cheryl 1
33273 Sara 1
33489 Denise 1
37092 Jennifer 1
37764 Jimmy 1
38540 Katherine 1
42467 Ernest 1
43240 Diana 1
43398 Steve 1
43595 Adam 1
45685 Bobby 1
46144 Sharon 1
46468 Timothy 1
46604 Christina 1
47156 Kelly 1
47921 Lillian 1
50560 Brian 1
52350 Teresa 1
53451 Jeffrey 1
53597 Rose 1
53768 Douglas 1
54015 Carolyn 1
54510 Paula 1
55415 Amy 1
56039 Teresa 1
56103 Kelly 1
56338 Jose 1
57195 Beverly 1
57873 Diana 1
58086 Debra 1
58167 David 1
58543 Rachel 1
59871 Martin 1
59895 Martha 1
60177 Brian 1
61093 Mark 1
61102 Kenneth 1
61206 Lillian 1
61769 Marie 1
63263 Dorothy 1
63684 Randy 1
63730 Sarah 1
63803 Carolyn 1
63961 Anna 1
64341 Virginia 1
64882 Roy 1
68178 Gloria 1
70499 Dennis 1
72321 Julie 1
72763 Julie 1
73267 Jeremy 1
73676 Linda 1
74320 Pamela 1
78615 Kathryn 1
79612 Tina 1
81652 Albert 1
83308 Roy 1
84739 Alan 1
84938 Judy 1
85094 Matthew 1
86142 Douglas 1
87040 Craig 1
87885 Gregory 1
88069 Jean 1
88083 Anna 1
88084 Alan 1
88858 Bruce 1
89514 Jeffrey 1
89903 Katherine 1
90276 Joyce 1
90369 Christina 1
91620 Debra 1
92239 Shirley 1
92920 Louis 1
94337 Lillian 1
94676 Patrick 1
94746 Adam 1
96521 Christine 1
96773 Angela 1
97338 Amy 1
98785 Rose 1
99101 Timothy 1
99165 Nancy 1
But this works:
SELECT H.hacker_id AS hacker_id, H.name AS name, COUNT(C.challenge_id) AS total
FROM Hackers H
JOIN Challenges C ON H.hacker_id = C.hacker_id
GROUP BY H.hacker_id, H.name
HAVING
--
total IN
(SELECT t0.total
FROM
(SELECT count(*) AS total
FROM challenges
GROUP BY hacker_id) t0
GROUP BY t0.total
HAVING count(t0.total) = 1)
-- instead of 'COUNT(total) = 1'
OR
total = (
SELECT MAX(amount) FROM (
SELECT COUNT(challenge_id) AS amount FROM Challenges
GROUP BY hacker_id
) t
)
ORDER BY total DESC, hacker_id;
Output:
5120 Julia 50
18425 Anna 50
20023 Brian 50
33625 Jason 50
41805 Benjamin 50
52462 Nicholas 50
64036 Craig 50
69471 Michelle 50
77173 Mildred 50
94278 Dennis 50
96009 Russell 50
96716 Emily 50
72866 Eugene 42
37068 Patrick 41
12766 Jacqueline 40
86280 Beverly 37
19835 Joyce 36
38316 Walter 35
29483 Jeffrey 34
23428 Arthur 33
95437 George 32
46963 Barbara 31
87524 Norma 30
84085 Johnny 29
39582 Maria 28
65843 Thomas 27
5443 Paul 26
52965 Bobby 25
77105 Diana 24
33787 Susan 23
45855 Clarence 22
33177 Jane 21
7302 Victor 20
54461 Janet 19
42277 Sara 18
99388 Mary 16
31426 Carlos 15
95010 Victor 14
27071 Gerald 10
90267 Edward 9
72609 Bobby 8
So, why i can't use just this
COUNT(total) = 1
instead of this large condition:
total IN
(SELECT t0.total
FROM
(SELECT count(*) AS total
FROM challenges
GROUP BY hacker_id) t0
GROUP BY t0.total
HAVING count(t0.total) = 1)
Because in your context total is just an alias to a aggregate result, not a value. In general you must repeat the COUNT(C.challenge_id) part but it will not help you here as COUNT(COUNT(C.challenge_id)) = 1 is clear wrong.
There are solutions to it like putting the aggregation dataset result in a temp table/table variable or using a CTE, that's is why a subquery works.
Disclaimer: don't chequed your query for correctness but that IN sounds ugly and using a CTE can possible be a better approach.
Below an example solution using MS SQL, sorry, don't get a MySql running right now nut don't used CTE =)
create table dbo.HACKER
(
hacker_id int,
name varchar(100)
)
GO
create table dbo.CHALLENGE
(
challenge_id int,
hacker_id int
)
GO
insert into dbo.HACKER
(hacker_id,name)
values
(5077,'Rose')
,(21283,'Angela')
,(62743,'Frank')
,(88255,'Patrick')
,(96196,'Lisa')
insert into dbo.CHALLENGE
(challenge_id, hacker_id)
values
(61654,5077)
,(58302,21283)
GO
--drop table #Temp
--drop table #totalsToExclude
select hk.hacker_id, hk.name, x.total
into #Temp
from dbo.HACKER hk
join (select ch.hacker_id, count(*) as total from dbo.CHALLENGE ch group by ch.hacker_id) as x
on x.hacker_id = hk.hacker_id
select * from #Temp
declare #maxTotal as int =(select max(total) from #temp)
select #maxTotal
select t.total, count(*) as [Count_total]
into #totalsToExclude
from #temp t
group by t.total
having(count(*) >1)
delete tx from #totalsToExclude tx where tx.total = #maxTotal
select * from #totalsToExclude
select * from #Temp t
where t.total not in (select t.total from #totalsToExclude)
order by t.total desc, t.hacker_id

In SQL select and count all entries where with same value in column

I have a table like in an mysql-database.
id section number name
1 A 1234 fred
2 B 5678 mo
3 B 1234 fred
4 C 8901 lou
5 A 8901 lou
6 A 2345 lee
7 B 2345 lee
8 C 2345 lee
9 A 6789 paul
10 B 1234 fred
This is my table and i need to get all numbers is in more then one section with the name of the section. like this
id section number name
1 A 1234 fred
3 B 1234 fred
Fred is in section A and B.
In my solution i get this
id section number count name
1 A 1234 1 fred
3 B 1234 2 fred
6 A 2345 1 lee
7 B 2345 1 lee
8 C 2345 1 lee
2 B 5678 1 mo
9 A 6789 1 paul
4 C 8901 1 lou
5 A 8901 1 lou
in my way i try is sql select
SELECT id, section, number, count(number) AS count, name
FROM table
GROUP BY section, number
ORDER BY number
Is is the right way? How can i improve that for mor number columns, if have 3 numbercolumns
id section number1 number2 number3 name
Now i do this with 3 sql-statements

Select rows from mysql table with limit by group

I have the table
id code name
=====================
1 30100 John
2 30100 Andrew
3 30100 Sandy
4 29145 Mike
5 29145 Tony
6 29145 Laura
7 29145 Henry
8 00124 Michael
9 00124 Teddy
10 13405 Andy
11 09325 Patrick
I want to select only 2 names grouped by code and get this result.
id code name
=====================
1 30100 John
2 30100 Andrew
4 29145 Mike
5 29145 Tony
8 00124 Michael
9 00124 Teddy
10 13405 Andy
11 09325 Patrick
Can somebody help me to make such query/queries?
Thanks
SELECT id, code, name
FROM TableName a
WHERE
(
SELECT count(*)
FROM TableName as f
WHERE f.code = a.code and a.id >= f.id
) <= 2
ORDER BY id, code, name
SQLFiddle Demo