I will list sample blog posts. There are editors who write these blog posts. I am listing these blog posts randomly. I want the blog posts belonging to the ids of the editors in the $onlineeditor directory to be sorted randomly first, and then the blog posts that do not have the editors id in $onlineeditor to be sorted randomly. How can I do that?
| id | user_id |
|---|---|
| 1 | 100 |
| 2 | 105 |
| 3 | 145 |
| 4 | 155 |
| 5 | 187 |
| 6 | 200 |
| 7 | 210 |
$onlineeditor = [100, 112, 145, 187];
$query = $this->db->query("SELECT * FROM blog ORDER BY rand('.$_SESSION['seed']') ASC limit $pageView, $pageLimit")->fetchAll(PDO::FETCH_ASSOC);
I want the query output to be like this
random { blog id: 3, 1, 5}
random { blog id: 7, 2, 5, 6, 4}
Advertisement
Answer
in PHP:
$ed = implode(',',$onlineeditor)
then, in SQL:
SELECT *
FROM blog
ORDER BY
case when user_id in ($ed) then 0 else 1 end,
rand('.$_SESSION['seed']'