Skip to content

Instantly share code, notes, and snippets.

@lavarini
Created September 20, 2013 13:58
Show Gist options
  • Save lavarini/6637997 to your computer and use it in GitHub Desktop.
Save lavarini/6637997 to your computer and use it in GitHub Desktop.
$res = $conn->query('
SET @num := 0, @genre := "";
SELECT
id_artista,
artista,
art_url,
genero_url,
genero,
(
SELECT id_foto
FROM letras.fotos AS f
WHERE f.id_artista = id_artista
ORDER BY RAND() LIMIT 1
) AS id_foto
FROM (
SELECT *,
@num := if(@genre = genero_url, @num + 1, 1) as total,
@genre := genero_url as dummy
FROM(
SELECT
a.id_artista,
a.artista,
a.dns AS art_url,
g.dns AS genero_url,
g.genero
FROM letras.artistas_generos AS ag
INNER JOIN letras.artistas AS a ON a.id_artista = ag.id_artista
INNER JOIN letras.generos AS g ON g.id_genero = ag.id_genero
INNER JOIN views_artistas_pt AS vap ON a.id_artista = vap.id_artista
WHERE g.topo = 1 and ag.principal = 1
ORDER BY g.genero ASC, vap.semanahits DESC
) AS y
) AS x WHERE x.total <= 5;
');
$genres = $res->fetchAll(\PDO::FETCH_ASSOC);
$res->closeCursor();
$res = $conn->query('SET @num := 0, @genre := "";');
$res = $conn->query('
SELECT
id_artista,
artista,
art_url,
genero_url,
genero
FROM (
SELECT *,
@num := if(@genre = genero_url, @num + 1, 1) as total,
@genre := genero_url as dummy
FROM(
SELECT
a.id_artista,
a.artista,
a.dns AS art_url,
g.dns AS genero_url,
g.genero
FROM letras.artistas_generos AS ag
INNER JOIN letras.artistas AS a ON a.id_artista = ag.id_artista
INNER JOIN letras.generos AS g ON g.id_genero = ag.id_genero
INNER JOIN views_artistas_pt AS vap ON a.id_artista = vap.id_artista
WHERE g.topo = 1 and ag.principal = 1
ORDER BY g.genero ASC, vap.semanahits DESC
) AS y
) AS x WHERE x.total <= 5;
');
$artistas = $res->fetchAll(\PDO::FETCH_ASSOC);
$list = array();
// Apenas os IDs dos artistas
$list = array_map(function($artista) {
return $artista['id_artista'];
}, $artistas);
$res = $conn->query('
SELECT
id_artista,
(
SELECT id_foto
FROM letras.fotos AS f
WHERE f.id_artista = id_artista
ORDER BY RAND() LIMIT 1
) AS foto
FROM letras.artistas
WHERE id_artista IN(' . implode(',', $list) . ')
');
$fotos = $res->fetchAll(\PDO::FETCH_ASSOC);
$res->closeCursor();
// Merge (fazendo)
array_map(function($a, $b) {
if ($a['id_artista'] == $b['id_artista']){
return $merge
}
}, $artistas, $fotos);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment