jeudi 19 mars 2015

MYSQL distinct random join query

I have two tables



albums fotos
------ -----
id id
titulo url
fk_album


I would like to know how I can select form all albums that are not empty random photos... and must be distinct... For example:



albums fotos
------ ------
1 A 1 my_url_1 2
2 B 2 my_url_2 2
3 C 3 my_url_3 1
4 D 4 my_url_4 2
5 E 5 my_url_5 3
6 my_url_6 3


I want results like these: (distinct, random & join)



result0 ->
albums.id -> 2
albums.titulo -> B
fotos.url -> my_url_2 (this must be random)
result1 ->
albums.id -> 3
albums.titulo -> C
fotos.url -> my_url_6 (this must be random)
result2 ->
albums.id -> 1
albums.titulo -> A
fotos.url -> my_url_3 (the album only have this foto url)


I have tried many things but actually I can only get the distinct albums with photos but without random values it.


Aucun commentaire:

Enregistrer un commentaire