I am developing a voting system.
I am facing issue in ordering.
Basically i want to get top ranking and maximum nominee first. I used "totalUserVoted" and "totalRating" in Descending order on both condition but my query ordering "totalUserVoted".
I am expecting the result in this order.
Here is my sql query.
SELECT
(SELECT (((SUM(`design`)*4)+(SUM(`usability`)*3)+(SUM(`creativity`)*2)+(SUM(`content`))*1))/ count(`nominee_id`) / 10
FROM `sk_award_nominee_rating`
WHERE `sk_award_nominee_rating`.`nominee_id`=`sk_award_nominee`.`nominee_id`) AS totalRating,
(SELECT count(`nominee_id`)
FROM `sk_award_nominee_rating`
WHERE `sk_award_nominee_rating`.`nominee_id`=`sk_award_nominee`.`nominee_id`) AS totalUserVoted,
`sk_award_nominee`.*,
`sk_user`.`username`,
`sk_user`.`email`,
`sk_user_profile`.`f_name`,
`sk_user_profile`.`m_name`,
`sk_user_profile`.`l_name`,
`sk_user_profile`.`address`
FROM `sk_award_nominee`
LEFT JOIN `sk_user` ON `sk_user`.`user_id`=`sk_award_nominee`.`user_id`
LEFT JOIN `sk_user_profile` ON `sk_award_nominee`.`user_id`=`sk_user_profile`.`user_id`
WHERE `sk_award_nominee`.`status` = 1
AND DATE(approval_date) = '2016-02-22'
ORDER BY `totalUserVoted` DESC,
`totalRating` DESC
Something like this ?
ORDER BY totalUserVoted DESC, totalRating DESC
PHP MySQL Order by Two Columns
Related
I need help with mysql order by, i have this query
SELECT
videos_views.videos_views_id,
videos_views.videos_views_date,
SUM(IF(MONTH(videos_views.videos_views_date) = 2, videos_views.videos_views_total, 0)) AS total_view,
videos.videos_id,
videos.videos_title,
videos.videos_description,
videos.videos_author_list_id,
author_list.author_list_name,
author_list.author_list_id
FROM videos
LEFT OUTER JOIN author_list
ON videos.videos_author_list_id = author_list.author_list_id
LEFT OUTER JOIN videos_views
ON videos.videos_id = videos_views.videos_views_id
WHERE author_list.author_list_video_type = 1
AND videos.videos_id >= 51108
GROUP BY videos_views.videos_views_id,
videos.videos_id
ORDER BY CASE WHEN MONTH(videos_views.videos_views_date) = 2 THEN SUM(videos_views.videos_views_total) END DESC,
CASE WHEN MONTH(videos_views.videos_views_date) <> 2 THEN videos.videos_id END DESC
LIMIT 11
And it returns the following results
If i use the first order by using DESC or ASC returns the same result above.
And i use the second order by using ASC returns the result bellow:
And I need the query to return the values in the following order bellow
Please Help me
Your results suggest that you want:
order by total_views desc, videos_id desc
You can reference column aliases (safely) in the order by.
I'm trying to join two tables so I can easily order it, as one contains the names of items and the other doesn't. The tables:
user_games: UGID, UID, APPID, playtime
games_steam: APPID, name
my SQL so far:
SELECT user_games.*
FROM user_games
JOIN games_steam ON user_games.APPID = games_steam.APPID
WHERE user_games.UID = '76561197996836099'
GROUP BY user_games.APPID
ORDER BY games_steam.name ASC
LIMIT 0, 30
Just no idea how to get the name column into this aswell.
Is this what you are looking for?
SELECT user_games.*, games_steam.name
FROM user_games
JOIN games_steam
ON user_games.APPID = games_steam.APPID
WHERE user_games.UID = '76561197996836099'
GROUP BY user_games.APPID
ORDER BY games_steam.name ASC
LIMIT 0, 30
select tbl_c_food_veg.pk_veg_id,tbl_c_food_veg.var_desc,tbl_c_food_veg.var_title ,tbl_c_food_veg_img.var_img,( select tbl_c_food_non_veg.pk_non_veg_id,tbl_c_food_non_veg.var_desc,tbl_c_food_non_veg.var_title,tbl_c_food_non_veg_img.var_img from tbl_c_food_non_veg left join tbl_c_food_non_veg_img on tbl_c_food_non_veg.pk_non_veg_id=tbl_c_food_non_veg_img.fk_non_veg_id where
tbl_c_food_non_veg.fk_cat_id=8 and tbl_c_food_non_veg.pk_non_veg_id!=0 group by tbl_c_food_non_veg_img.fk_non_veg_id order by tbl_c_food_non_veg.pk_non_veg_id desc limit 2 ) as non_veg,( select tbl_c_food_drinks.pk_drinks_id, tbl_c_food_drinks.var_desc, tbl_c_food_drinks.var_title, tbl_c_food_drinks_img.var_img from tbl_c_food_drinks left join tbl_c_food_drinks_img on tbl_c_food_drinks.pk_drinks_id=tbl_c_food_drinks_img .fk_drinks_id where
tbl_c_food_drinks.fk_cat_id=8 and tbl_c_food_drinks.pk_drinks_id!=0 group by tbl_c_food_drinks_img.fk_drinks_id order by tbl_c_food_drinks.pk_drinks_id desc limit 2 ) as drinks from tbl_c_food_veg left join tbl_c_food_veg_img on tbl_c_food_veg.pk_veg_id=tbl_c_food_veg_img.fk_veg_id where
tbl_c_food_veg.fk_cat_id=8 and tbl_c_food_veg.pk_veg_id!=0 group by tbl_c_food_veg_img.fk_veg_id order by tbl_c_food_veg.pk_veg_id desc limit 2
In your nested sub queries for retrieving non_veg and drinks you use limit 2 so most likely the subquery will return 2 rows, but you can only use one row. Change the limit to limit 1
and try using aliases as Barranka says it wil be much easier to read your query
I was thinking a way to using one query with a subquery instead of using two seperate queries.
But turns out using a subquery is causing multiple requests for each row in result set. Is there a way to limit that count subquery result only one with in a combined query ?
SELECT `ad_general`.`id`,
( SELECT count(`ad_general`.`id`) AS count
FROM (`ad_general`)
WHERE `city` = 708 ) AS count,
FROM (`ad_general`)
WHERE `ad_general`.`city` = '708'
ORDER BY `ad_general`.`id` DESC
LIMIT 15
May be using a join can solve the problem but dunno how ?
SELECT ad_general.id, stats.cnt
FROM ad_general
JOIN (
SELECT count(*) as cnt
FROM ad_general
WHERE city = 708
) AS stats
WHERE ad_general.city = 708
ORDER BY ad_general.id DESC
LIMIT 15;
The explicit table names aren't required, but are used both for clarity and maintainability (the explicit table names will prevent any imbiguities should the schema for ad_general or the generated table ever change).
You can self-join (join the table to itself table) and apply aggregate function to the second.
SELECT `adgen`.`id`, COUNT(`adgen_count`.`id`) AS `count`
FROM `ad_general` AS `adgen`
JOIN `ad_general` AS `adgen_count` ON `adgen_count`.city = 708
WHERE `adgen`.`city` = 708
GROUP BY `adgen`.`id`
ORDER BY `adgen`.`id` DESC
LIMIT 15
However, it's impossible to say what the appropriate grouping is without knowing the structure of the table.
I want to select the last 10 rows but the order should remain the asc only.
Its like displaying the last page in a forum but the post are still aligned in the right order.
I tried to do this
SELECT users.display,appreply.*,users.userid,users.avtar FROM appreply
LEFT JOIN users ON users.userid=appreply.userid
WHERE appreply.appid='$appid'
ORDER by apprepid DESC
LIMIT 10
But as i said this disturbs the order.
Please help me
try
SELECT X.* FROM
(
SELECT users.display,a.apprepid apprepid_ar, a.appid appid_ar, a.reply reply_ar, a.userid userid_ar, a.browser browser_ar, a.os os_ar, a.time time_ar,users.userid,users.avtar FROM appreply a
LEFT JOIN users ON users.userid=a.userid
WHERE a.appid='$appid'
ORDER by a.apprepid DESC
LIMIT 10
) X ORDER BY X.apprepid_ar ASC
You might be having an auto-increment field in the table, which is usually 'Id'.
Instead of using the DESC operator on the apprepid field, use the Id field as:
SELECT users.display,appreply.*,users.userid,users.avtar FROM appreply
LEFT JOIN users ON users.userid=appreply.userid
WHERE appreply.appid='$appid'
**ORDER by id DESC**
LIMIT 10 ORDER BY apprepid ASC