I've this query, and everytime I run it it gives always the same result. But I'm ordering only by location_id a random result. So it should still keep random but just order by location_id. Right?
$sql = 'SELECT location_id, ad_id, ad_code
FROM (
SELECT al.location_id, a.ad_id, a.ad_code
FROM ' . $this->ad_locations_table . ' al
LEFT JOIN ' . $this->ads_table . ' a
ON (al.ad_id = a.ad_id)
WHERE a.ad_enabled = 1
AND (a.ad_end_date = 0
OR a.ad_end_date > ' . time() . ")
$sql_where_views
$sql_where_clicks
AND " . $this->db->sql_in_set('al.location_id', $ad_locations) . '
ORDER BY (' . $this->sql_random() . ' * a.ad_priority) DESC
) z
ORDER BY z.location_id';
And I've saw that if I remove the ORDER BY z.location_id' it will work great and it will random. So the order by seems to be ordering not only by location_id but ad_id too -.-
I've made some changes to the query above to simplify:
$sql = 'SELECT al.location_id, a.ad_id, a.ad_code
FROM ' . $this->ad_locations_table . ' al
LEFT JOIN ' . $this->ads_table . ' a
ON (al.ad_id = a.ad_id)
WHERE a.ad_enabled = 1
AND (a.ad_end_date = 0
OR a.ad_end_date > ' . time() . ")
$sql_where_views
$sql_where_clicks
AND " . $this->db->sql_in_set('al.location_id', $ad_locations) . '
GROUP BY al.location_id, a.ad_id, (RAND() * a.ad_priority) DESC';
It gives the exact same result, and MySQL 5.7+ asks for a.ad_id on the groupby orelse it gives error "sql_mode=only_full_group_by".
So right now I'm using this code, similar to the above, just change the last line GROUP BY to ORDER BY:
$sql = 'SELECT al.location_id, a.ad_id, a.ad_code
FROM ' . $this->ad_locations_table . ' al
LEFT JOIN ' . $this->ads_table . ' a
ON (al.ad_id = a.ad_id)
WHERE a.ad_enabled = 1
AND (a.ad_end_date = 0
OR a.ad_end_date > ' . time() . ")
$sql_where_views
$sql_where_clicks
AND " . $this->db->sql_in_set('al.location_id', $ad_locations) . '
ORDER BY al.location_id, (' . $this->sql_random() . ' * a.ad_priority) DESC';
And now the query works and gives random results. I still doesn't understand why the 1st query doesn't work... Do anyone knows why?
Thanks in advance!
Aucun commentaire:
Enregistrer un commentaire