I want to have a SQL result look like this (match result query):
ID Arena Winner Loser Winner_score Loser_score
-----------------------------------------------------------------
1 1 Johnny Mark 16 8
2 2 Andrew Luke 16 7
Here are my tables (simplified)
Player_tbl
ID Player
-------------
1 Johnny
2 Mark
3 Andrew
4 Luke
Match_tbl
ID Match Arena
----------------------
1 Match 1 1
2 Match 2 2
Match_results_tbl
ID Match player_id player_score
-----------------------------------------
1 Match 1 1 16
2 Match 1 2 8
1 Match 2 3 16
2 Match 2 4 7
I am wondering how to structure my query to have the desired result.
Ok, I figured out my own issue. 1st, the match_results must be put in a table with a different structure, and 2nd, the inner sql query needed to be adjusted to pull from the proper database tables.
Related
Whats the best way to check if different groups of rows in a table with the same GroupID such as different teams have a SINGLE captain? Captain Identifier for example could be '10', so its crucial that it goes through multiple records with the same groupID and checks to see if theres ONLY ONE record with the positionID as '10'. I need it to do it for all teams, i.e all groupID's
-------------------------------------------------
ID | Group ID | Name | Position|
-------------------------------------------------
1 1 John 3
2 1 jim 3
3 1 Hahn 4
4 1 Mary 4
5 1 Moe 4
6 1 Charlie 10
7 2 taylor 4
8 2 Geoff 4
9 2 adam 4
10 2 cam 10
11 3 sharon 2
12 3 tony 4
13 3 steve 3
14 3 eve 4
15 3 gwen 10
--------------------------------
So what I need it to do is check that every groupID only had ONE 10 as the position.
Thanks in advance guys. Check out the image link at the bottom.
im using mysql btw
Sorry if this is badly described
If I understood you - I think you need something like :
select groupId, sum(case when positionID='10' then 1 else 0 end) as captains
from tbl_name
group by groupID
having captains = 1
am I close???
If I understood you correctly, you want an indication that will tell you whether the groupID had more then one captain.
So what you need is this:
select groupID,case when cnt = 1 then 1 else 0 end as IND_ONE_CAPTAIN from (
select groupID, sum(case when positionid = '10' then 1 else 0 end) as cnt
from players
group by groupID)
Now IND_ONE_CAPTAIN columns consist 1 or 0, 1 is for the group id only had 1 captain, and 0 is when they had more.
I'm stuck with the following problem.
I have a website with for example supermarket shopping items. People can search the website for items.
Now I want on the search result page at the top 2 items to be displayed that I have selected to be on offer. There can be lots more items on offer.
So for example, someone would search for shampoo, the query would display all the shampoo items in the database table but I want just 2 shampoo offer items at the top of the query. There could be 2 or more shampoo offers in the database table, then the other would just not be shown.
Example with names :
Table:
id name C D
----------------------------------
1 Jack 1 1
2 Joe 1 1
3 Dave 3 0
4 Sue 1 0
5 Mike 1 1
6 Steve 4 0
7 David 1 0
8 Susan 4 1
9 Marc 1 1
10 Ronald 4 1
11 Michael 4 1
EXAMPLE 1
Query :
WHERE C = 1 AND D = 1 (But only maximum of 2 'D' records, these 2 'D' records show at the top of the result)
Desired Query Result :
id name C D
----------------------------------
1 Jack 1 1
2 Joe 1 1
4 Sue 1 0
7 David 1 0
EXAMPLE 2
Query :
WHERE C = 4 AND D = 1 (But only maximum of 2 'D' records, these 2 'D' records show at the top of the result)
Desired Query Result :
id name C D
----------------------------------
8 Susan 4 1
10 Ronald 4 1
6 Steve 4 0
I hope this explains my goal what I'm trying to achieve.
Many thanks for any help or suggestions!
That's two queries that you can combine with UNION ALL:
select * from mytable where c = 1 and d = 1 limit 2
union all
select * from mytable where c = 1 and d = 0
order by d desc;
UPDATE: If you want to have the two rows chosen randomly, then order by RAND(). (Without an ORDER BY the rows are chosen arbitrarily, which means it's not guaranteed to get the same two rows picked again when re-running the query ‐ but it's quite likely.) As we need an ORDER BY for a partial query (the first query in the complete union-alled query), we must use parentheses, because otherwise only one ORDER BY would be allowed, namely for the complete query at the query's end.
(select * from mytable where c = 1 and d = 1 order by rand() limit 2)
union all
(select * from mytable where c = 1 and d = 0)
order by d desc;
SQL fiddle: http://sqlfiddle.com/#!9/ed53f6/3.
I need some direction on how to create this MySQL query.
I have a table that looks like this.
id group name value
1 1 user mike
2 1 setting 1
3 2 user joe
4 2 setting 2
5 3 user jill
6 3 setting 1
7 4 user mark
8 4 setting 1
9 4 other 22
I would like to format the query to group users that have identical settings (IE mike and jill would be grouped in this example, not mark because of "other")
At the end of the day I am trying to consolidate and make the table look like this. If I can figure out the query to properly group them ,I will use PHP to combine the values and save it back to the DB.
id group name value
1 1 user mike OR jill
2 1 setting 1
3 2 user joe
4 2 setting 2
7 4 user mark
8 4 setting 1
9 4 other 22
Thank you!
I have two tables, one of a list of competition results, and one with ELO ratings (based on previous competitions).
Fetching the list of competitors for an arbitrary competition is trivial enough, but I also need to get the most recent rating value for them.
score:
id | eventid | competitorid | position
1 1 1 1
2 1 2 2
3 1 3 3
4 2 2 1
5 2 3 2
6 3 1 1
7 3 3 2
8 3 2 3
rating:
id | competitorid | rating
1 1 1600
2 2 1500
3 3 1500
4 2 1600
5 3 1590
Expected output for a query against score.eventid = 3 would be
id | competitorid | position | rating
6 1 1 1600
7 3 2 1590
8 2 3 1600
At the moment my code looks like:
SELECT score.scoreID, score.competitorID, score.position,
rating.id, rating.rating
FROM score, rating
WHERE score.competitorid = rating.competitorid
AND score.eventid = 3
ORDER BY score.position
which gives an output of
id | competitorid | position | rating.id | rating
6 1 1 1 1600
7 3 2 2 1500
7 3 2 4 1590
8 2 3 3 1500
8 2 3 5 1600
basically it's showing the data from the score table for that correct event, but giving me a row for every rating available against that competitorID unfortunately I have no idea where to build in the DISTINCT statement or how to limit it to the most recent result.
MySQL noob, and managed DISTINCT statements, but not with joins. Unfortunately most previous questions seemed to deal with getting distinct results from a single table, which is not quite what I'm after. Thanks!
One way to get the rating is with a correlated subquery:
SELECT s.scoreID, s.eventID, s.competitorID, s.position,
(select r.rating
from rating r
where s.competitorID = r.competitorID
order by r.id desc
limit 1
) as rating
FROM score s
WHERE s.eventID = 3
ORDER BY s.position;
I'm not sure what ratingprid is, so this only includes the rating.
i am having table Category with the columns like
id(AutoIncrement),Parent_id,Level,Name
initially for level 1 datas has Parent_id is 0. autoincrement id will be Parent_id for next levels .my table table table datas will bw like this
id Parent_id Level Name
1 0 1 Indian
2 0 1 International
3 0 1 Nri
4 1 2 BC
5 2 2 Christian
6 2 2 Muslim
7 4 3 MBC-1
8 7 4 OBC-2
9 1 2 FC
i want to show records in this format (its like a tree view)
id Parent_id Level Name
1 0 1 Indian
4 1 2 BC
7 4 3 MBC-1
8 7 4 OBC-2
9 1 2 FC
5 2 2 Christian
6 2 2 Muslim
2 0 1 International
3 0 1 Nri
4 1 2 BC
Can any one should help me to get this arrangement of datas using sql Query?
If it does not have a set number of branches you likely want to loop over a query in your app or write an SP to obtain all nodes. Some good reading here:
http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/