Select specified items from rows at first priority mySql - mysql

I have a table users
id | Name |
1 John
2 Dwayne
3 Daniel
4 Ronaldo
5 Messi
6 Gareth
7 Leonardo
8 Brad
If i have id's in order of (7,5,1,3) and i want to select limit 6 then the expected output should be as following :-
id Name
7 | Leonardo
5 | Messi
1 | John
3 | Daniel
2 | Dwayne
4 | Ronaldo
What i did previously was this but did not get the expected output . Help!!
"select * from users ORDER BY FIELD(user_id,7,5,1,3) LIMIT 6"

Your query is close. It just doesn't take into account all the other user ids. Try this:
ORDER BY FIELD(user_id,7,5,1,3) > 0 desc,
FIELD(user_id,7,5,1,3)
LIMIT 6;
The problem with your version is that field() returns 0 when there is no match. And the 0s will be ordered first.

Related

Gaming Database Query for multiple players

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.

How to group this table column in MySQL?

I have a following data below:
Table:
Name Number
Sam 1
Sam 2
Sam 3
John 4
Lani 5
Mera 6
Now I want as a result like this format below
Result:
Name Number
Sam 1,2,3
John 4
Lani 5
Mera 6
How can I possibly do this? What I must going to use?
You can do this with group_concat():
select name, group_concat(number order by number) as numbers
from t
group by name;

How to check for one value in multiple rows

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.

Most recent distinct record from a joined MySQL table

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.

MySQL cyclically one row for each valid user

Here's the background, I have a table object containing :
id
user (id from user table)
date (date)
state (0|1)
And another table user containing :
id
state (0|1)
EX.:
Object
ID | USER | DATE | STATE
1 1 2012-10-01 1
2 2 2012-08-01 1
3 3 2012-10-01 1
4 3 2012-10-02 1
5 3 2012-09-01 1
6 3 2012-10-01 1
7 1 2012-10-02 1
8 2 2012-10-02 0
9 2 2012-10-02 1
10 1 2012-10-01 1
11 2 2012-10-01 1
12 3 2012-10-01 0
13 4 2012-11-11 1
Users
ID | STATE
1 | 1
2 | 1
3 | 1
4 | 0
I would make a request to display one object per valid state user cyclically, if object state is active, order by date DESC.
EX. result :
ID | USER | DATE | STATE
10 1 2012-11-01 1
9 2 2012-10-02 1
4 3 2012-10-02 1
7 1 2012-10-02 1
11 2 2012-10-01 1
3 3 2012-10-01 1
1 1 2012-10-01 1
2 2 2012-08-01 1
6 3 2012-10-01 1
5 3 2012-09-01 1
I can do simple queries but then I admit to not knowing how to do here.
EDIT 2012-11-22 :
Let trying another approch... With this query (thanks Michiel van Vaardegem), I have a list of users, in order that I want... the one with freshness object on top.
SELECT o.user
FROM object o
INNER JOIN users ON user.id = o.user
WHERE o.state=1 AND users.state=1
GROUP BY o.user
ORDER BY o.date DESC
Result is :
USER
1
2
3
NEW QUESTION APPROCH :
With this list of ids, the possible calculated count of rows, and user defined vars, do you think something could be done?
Exemple :
Assuming this list, SELECT * FROM object WHERE user IN
($thislist) AND state=1 ORDER BY date DESC
For each object row depending of user (here I don't know how to explain in mysql)
incrementing a rank var with the userposition(in cycle) + usercount
ORDER BY rank LIMIT n,x that result
I whant to rank my objects in a user cycle, like :
object from user 1,2,3,1,2,3,1,3,3,1,1,1,1,1,1,1,1,etc... if exist
Do this new approch algo be realizable in a mysql query, and if possible how... that is the great question?
Can anybody see a solution only in MySQL?
Thanks in advance
Something like:
SELECT o.id, o.user, o.date, 1 AS state
FROM object o
INNER JOIN user ON u.id = o.user
WHERE o.state = 1 AND u.state = 1
GROUP BY o.user, o.date
ORDER BY o.user, o.date DESC
I'm not sure, but if I read it correctly, you only want records where the state in both tables is '1', correct?
And you want to sort first on user and then on the date desc?