Note that there are some explanatory texts on larger screens.

plurals
  1. POMysql GROUP BY in LEFT JOIN and ORDER BY
    primarykey
    data
    text
    <p>I have this query which works fine </p> <pre><code> $query = "SELECT beva_post.*, CONVERT_TZ(beva_post.date_published, '-00:00', '+00:00') as moment, beva_rubrique.titre_en, beva_user.image_user FROM beva_post LEFT JOIN beva_user ON beva_user.id_user = beva_post.id_user LEFT JOIN beva_rubrique ON beva_rubrique.id_rubrique = beva_post.id_rubrique LEFT JOIN beva_post_like ON beva_post_like.id_post = beva_post.id_post WHERE beva_post.id_user != '' AND beva_post.flag = 'true' "; $query .= "ORDER BY beva_post.id_post DESC "; $query .= "LIMIT 50"; </code></pre> <p>But I would like to ORDER this query not by id_post DESC but by id_post that have the most like by id_user (DESC) in the table beva_post_like</p> <p>The beva_post_like table looks like this :</p> <pre><code> +-------------+--------------+ | id_post | id_user | +-------------+--------------+ | 1 | 20 | | 1 | 11 | | 1 | 10 | | 2 | 20 | | 2 | 50 | | 1 | 15 | +-------------+--------------+ </code></pre> <p>I tried with GROUP BY in the LEFT JOIN beva_post_like but it does not work !</p> <p>Do I have to use more queries ?</p> <p>Thanks for your help...</p> <p>Chris</p> <p>After your answer I did this but I still have some posts with 'like' at the end</p> <pre><code> $query = "SELECT beva_post.*, COUNT(beva_post.id_post) as cpt, CONVERT_TZ(beva_post.date_published, '-00:00', '+00:00') as moment, beva_rubrique.titre_en, beva_user.image_user FROM beva_post LEFT JOIN beva_user ON beva_user.id_user = beva_post.id_user LEFT JOIN beva_rubrique ON beva_rubrique.id_rubrique = beva_post.id_rubrique LEFT JOIN beva_post_like ON beva_post_like.id_post = beva_post.id_post WHERE beva_post.id_user != '' AND beva_post.flag = 'true' "; $query .= "GROUP BY beva_post.id_post "; $query .= "ORDER BY cpt DESC "; $query .= "LIMIT 50"; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload