'order by using join in php

I have two tables Products and Userproducts. Products table having columns: id,name,description and Userproducts table have following columns id,user_id,product_id. Current query is

SELECT Products.id,Userproducts.user_id
FROM Products 
LEFT JOIN UserProducts ON Productsid = Userproducts.product_id;

Now i want to show those products at the top whose users_id = 1 and other products will come after users products



Solution 1:[1]

You can add conditional order by clause as

SELECT p.id,u.user_id 
FROM Products p
LEFT JOIN UserProducts u ON p.id = u.product_id
ORDER BY u.user_id = 1 desc, u.user_id asc

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1