May 21, 201511 yr 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
May 21, 201511 yr 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.
May 21, 201511 yr Author Thanls nahosting, Am not really cleared, can you please white out the syntax for me? Thanks grately.
May 22, 201511 yr 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 May 22, 201511 yr by Nillervision
May 26, 201511 yr 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
May 27, 201511 yr Author Thanks all, I really appreciate you. let me try this last solution. Thanks a lot
Create an account or sign in to comment