Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

How do I sort mysql table result in ORDER BY 2 columns

Featured Replies

Hello Friends,

I have a products table with several columns. On my Home page I want to display 30 items at random but all new Items to come first. Below are the codes I tried.

$sql = mysql_query("SELECT * FROM products WHERE status='Online' && nature='Yes' ORDER BY RAND() LIMIT 5"); //FOR ONLY NEW ITEMS

$sql = mysql_query("SELECT * FROM products WHERE status='Online' && nature!='Yes' ORDER BY RAND() LIMIT 35"); //FOR OTHER ITEMS

How do I make them into one line.

Thanks

Add a number in each select e.g. SELECT *, 1 as sortcolumn FROM products for new items and SELECT *, 2 as sortcolumn FROM products for other items and sort by that column to get the new ones at the top.

Use the UNION clause to join them.

As nahosting points out you can use UNION to execute two querystrings in one db call. But in order to get the 5 newest products (your first querystring) you should have a timestamp field for each product to be sorted by. When you set up your product table you can set a default value for the timestamp to "CURRENT_TIMESTAMP".

Note that the msql_query function is deprecated and taken out of PHP since v. 5.5. You should use PDO or msqli instead for your db queries.

Edited by Nillervision

This maybe help what you looking for the way to sort table by two columns,

SELECT
 `id`,
 `text`,
 `date`
 FROM
   (
   SELECT
     k.`id`,
     k.`text`,
     k.`date`,
     k.`match_order_id`,
     @[member="Row"] := @[member="Row"] + 1 as `date_order_id`
     FROM
     (
       SELECT
         t.`id`,
         t.`text`,
         t.`date`,
         @[member="Row"] := @[member="Row"] + 1 as `match_order_id`
         FROM
         (
           SELECT
             `art_id` AS `id`,
             `text`   AS `text`,
             `date`   AS `date`,
             MATCH (`text`) AGAINST (:string) AS `match`
             FROM int_art_fulltext
             WHERE MATCH (`text`) AGAINST (:string IN BOOLEAN MODE)
             LIMIT 0,101
         ) t,
         (
           SELECT @[member="Row"] := 0
         ) r
         ORDER BY `match` DESC
     ) k,
     (
       SELECT @[member="Row"] := 0
     ) l
     ORDER BY k.`date` DESC
   ) s
 ORDER BY (1/`match_order_id`+1/`date_order_id`) DESC

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.