2

Is it possible to make a query which selects one record that has the player_type 'IF' and 10 records that has the player_type 'Gold' which is ordered by 'cardweight' field / rand() desc? Thanks. Below are some attached screenshots of my table showing of some the player_type 'IF' and 'Gold' records.

Records with player_type as 'IF'

Records with Player Type as 'Gold'

essentially, I would like to combine

SELECT * 
FROM  `my_players` 
WHERE  `player_type` =  'Gold'
ORDER BY cardweight / RAND( ) DESC 
LIMIT 0 , 10

and

    SELECT * 
FROM  `my_players` 
WHERE  `player_type` =  'IF'
ORDER BY cardweight / RAND( ) DESC 
LIMIT 0 , 1

in one query that can be ran at once if possible.

5
  • Pretty unclear what you're asking. Post some sample data and intended output Commented Dec 18, 2016 at 11:57
  • @GurwinderSingh I've tried to make it a bit clearer now sorry. Commented Dec 18, 2016 at 12:03
  • What are the fields that you want in each query output? are they same? Commented Dec 18, 2016 at 12:04
  • It should return all the information held in each record, but I just want it that it randomly selects 10 records with the player_type 'gold' and 1 with the record with the player type 'IF' and is selected randomly dependent on the cardweight field - e.g. ORDER BY cardweight/rand() desc Commented Dec 18, 2016 at 12:09
  • 1
    You should explain why the obvious solution of union all does or does not meet your needs. Commented Dec 18, 2016 at 13:21

2 Answers 2

1

You use union all:

(SELECT * 
FROM  `my_players` 
WHERE  `player_type` =  'Gold'
ORDER BY cardweight / RAND( ) DESC 
LIMIT 0 , 10)
UNION ALL
(SELECT * 
FROM  `my_players` 
WHERE  `player_type` =  'IF'
ORDER BY cardweight / RAND( ) DESC 
LIMIT 0 , 1)
Sign up to request clarification or add additional context in comments.

Comments

0

You can do a UNION:

SELECT * 
FROM  `my_players` 
WHERE  `player_type` =  'Gold'
ORDER BY cardweight / RAND( ) DESC 
LIMIT 0 , 10

UNION

SELECT * 
FROM  `my_players` 
WHERE  `player_type` =  'IF'
ORDER BY cardweight / RAND( ) DESC 
LIMIT 0 , 1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.