mardi 29 juin 2021

SQL query with rand select

Please, can I have some help with a MySQL query? I have a list of 18 entries in a table with each a corresponding group, there are 6x groups. My requirement is to randomly pick 6 entries from that table and only take one entry per group.

Here is my current query, but the problem with this is it is not taking only 1 entry per group, I am getting 2 results for group 3 returned for example.

SELECT mask,group_id, icon, infoText FROM table_name ORDER BY RAND() LIMIT 6;

Current output when running above query, as you can see 2x results for group 1 are returned.

mask group_id icon infoText
2 1 dot blaba
7 1 dot blaba2
6 2 dot blaba3
4 3 dot blaba4
9 4 dot blaba5
10 5 dot blaba6

When I want it to something like this instead, when a random mask is selected but only 1 entry from that group

mask group_id icon infoText
1 1 dot blaba
2 2 dot blaba2
6 3 dot blaba3
3 4 dot blaba4
5 5 dot blaba5
12 6 dot blaba6



Aucun commentaire:

Enregistrer un commentaire