Perfiles con JOIN:
Código MySQL:
Ver original
SELECT u.rango, u.creditos, r.nombre, COUNT(DISTINCT t.id) AS temas_usuario, COUNT(DISTINCT d.id) AS descargas_usuario, COUNT(DISTINCT f.id) AS fotos_usuario, COUNT(DISTINCT e.id) AS estados_usuario FROM usuarios AS u LEFT JOIN rangos AS r ON u.rango = r.id LEFT JOIN temas AS t ON u.id = t.autor && t.status = '0' LEFT JOIN downloads AS d ON u.id = d.autor LEFT JOIN fotos AS f ON u.id = f.autor && f.status = '0' LEFT JOIN estados AS e ON u.id = e.autor && e.status = '0' WHERE u.id = '1'
Totales con JOIN:
Código MySQL:
Ver original
SELECT COUNT(DISTINCT u.id) AS usuarios_totales, COUNT(DISTINCT t.id) AS temas_totales, COUNT(DISTINCT d.id) AS desgargas_totales, COUNT(DISTINCT f.id) AS fotos_totales, COUNT(DISTINCT e.id) AS estados_totales FROM usuarios AS u LEFT JOIN temas AS t ON u.id = t.autor && t.status = '0' LEFT JOIN downloads AS d ON u.id = d.autor && d.status = '0' LEFT JOIN fotos AS f ON u.id = f.autor && f.status = '0' LEFT JOIN estados AS e ON u.id = e.autor WHERE u.activado = '1' && u.baneado = '0'
Ahora tengo dos usando subqueries, una de ellas contiene JOIN
Código MySQL:
Ver original
SELECT (SELECT count(id) FROM u_miembros WHERE activado = '1' AND baneado = '0') AS usuarios_totales, (SELECT count(id) FROM temas AS t LEFT JOIN usuarios AS u ON t.autor = u.id WHERE t.status = '0' && u.activado = '1' AND u.baneado = '0') as temas_totales, (SELECT count(id) FROM descargas AS d LEFT JOIN usuarios AS u ON d.autor = u.id WHERE t.status = '0' AND u.activado = '1' AND u.baneado = '0') as descargas_totales [...]
Y la otra, sin el uso de JOIN:
Código MySQL:
Ver original
SELECT * FROM (SELECT COUNT(id) AS usuarios_totales FROM usuarios WHERE activado = '1' && baneado = '0') AS ut, (SELECT COUNT(id) AS temas_totales FROM temas WHERE autor IN (SELECT id FROM usuarios WHERE activado = '1' && baneado = '0') && status = '0') AS tt, (SELECT COUNT(id) AS descargas_totales FROM descargas WHERE autor IN (SELECT id FROM usuarios WHERE activado = '1' && baneado = '0') && status = '0') AS dt, (SELECT COUNT(id) AS fotos_totales FROM fotos WHERE autor IN (SELECT id FROM usuarios WHERE activado = '1' && baneado = '0') && status = '0') AS ft, (SELECT COUNT(id) AS estados_totales FROM estados WHERE autor IN (SELECT id FROM usuarios WHERE activado = '1' && baneado = '0')) AS dt;
Todas devuelven la misma cantidad, pero no estoy seguro de cuál sea la más óptima en cada caso, ¿alguien puede orientarme?