random order for grouped and limited query - mysql

I have a query like
SELECT name,count(*) cnt group by b order by cnt desc limit 10;
So I will have a list of items like
Name Count
test 6
test2 4
test6 1
test23 1
test4 1
The problem is that there are 1000s results with cnt=1 and I would like to show them randomly, otherwise I'd always show more or less the same for every query. What I would like is a random order on the rows with same cnt value. Is it doable?

You can use RAND() in order by after your count
SELECT name,count(*) cnt from t
group by b order by cnt desc ,RAND()
limit 10;
So first rows will be ordered by cnt in descending order then for the same value of count (cnt) rows will be ordered randomly
Sample demo with provided dataset

Related

Select row with maximum value from table

I have a table with non-unique column auth_id. I need to select the auth_id value with maximum number of entries.
SELECT auth_id, cnt
FROM (SELECT auth_id, COUNT(auth_id) AS cnt
FROM articles_authors
GROUP BY auth_id) articles_num
WHERE cnt = (SELECT MAX(articles_num.cnt))
Here's the data example:
auth_id article_id
1 2
1 1
1 3
2 2
3 1
3 2
And the output:
auth_id cnt
1 3
But SQL doesn't see the alias table articles_num.
How do I make this WHERE clause with this alias?
Using a limit clause would be much simpler - you simply order a query according to some field, and then just take the first row:
SELECT auth_id, COUNT(auth_id) AS cnt
FROM articles_authors
GROUP BY auth_id
ORDER BY 2 DESC
LIMIT 1
Order your data in descending order in your inner query then just take the first one:
SELECT auth_id, cnt
FROM (
SELECT auth_id, COUNT(auth_id) AS cnt
FROM articles_authors
GROUP BY auth_id
ORDER BY cnt DESC
)
LIMIT 1
If I understand correctly, you actually want to get one row of the max of the count:
SELECT auth_id, count(auth_id) as cnt
FROM articles_authors
GROUP BY 1
ORDER BY 2 DESC
LIMIT 1
If more than one auth_id have same max count, we need to update this SQL
Try this.......
select auth_id, count(auth_id) as cnt
from articles_authors
group by auth_id
order by cnt DESC
limit 1;
Let me know if it resolves your issue

Fetching the last 3 rows in mysql in ascending order

I have a table named comments.I want to retrieve the last 3 rows in the ascending order of time.The following query fetches it in the descending order of time.What should I do to get the desired result?
select * from comments where id=1 order by time desc limit 0,3
Use a subquery:
select c.*
from (select c.*
from comments c
where id = 1
order by time desc
limit 0, 3
) c
order by time asc;

How to get the end of an Ascending list of records with a limit?

I'm fighting a bit with a query I'm building. Let's say I've got a DB table like this:
id | some_string
----------------
1 | 'lala'
2 | 'jeje'
3 | 'poopoo'
4 | 'wicked wicked'
I now want to get the last three records (2, 3, and 4) ordered ascending by key. I tried this:
SELECT * FROM tableName LIMIT 3 ORDER BY id ASC
This gets me the first three records, instead of the last three. I can of course also use the query below, which gets me the correct records, but then I don't get them in Ascending order:
SELECT * FROM tableName LIMIT 3 ORDER BY id DESC
Does anybody know how I can get the last three records in an ascending order? All tips are welcome!
select * from (
select * from table_name order by id desc limit 3
) last_3_rows
order by id
Sort on the resulting result set ie. do a select * from (<your query here>) order by id
This is a query inside another query. that reorders your query.(SQL - How to reorder a select query that uses the limit constraint)
select * FROM (SELECT * FROM tableName LIMIT 3 ORDER BY id DESC) AN_UNUSUAL_NAME ORDER BY id ASC

MySQL select top ten records with no duplicate uid

I have the following table (user_record) with millions of rows like this:
no uid s
================
1 a 999
2 b 899
3 c 1234
4 a 1322
5 b 933
-----------------
The uid can be duplicate .What I need is to show the top ten records(need inclued uid and s) with no duplicate uid order by s (desc). I can do this by two steps in the following SQL statements:
SELECT distinct(uid) FROM user_record ORDER BY s DESC LIMIT 10
SELECT uid,s FROM user_record WHERE uid IN(Just Results)
I just wana know is there a bit more efficient way in one statement?
Any help is greatly appreciated.
ps:I also have following the SQL statement:
select * from(select uid,s from user_record order by s desc) as tb group by tb.uid order by tb.s desc limit 10
but it's slow
The simpliest would be by using MAX() to get the highest s for every uid and sorted it based on the highest s.
SELECT uid, MAX(s) max_s
FROM TableName
GROUP BY uid
ORDER BY max_s DESC
LIMIT 10
SQLFiddle Demo
The disadvantage of the query above is that it doesn't handles duplicates if for instance there are multiple uid that have the same s and turn out to be the highest value. If you want to get the highest value s with duplicate, you can do by calculating it on the subquery and joining the result on the original table.
SELECT a.*
FROM tableName a
INNER JOIN
(
SELECT DISTINCT s
FROM TableName
ORDER BY s DESC
LIMIT 10
) b ON a.s = b.s
ORDER BY s DESC

MySQL - Select 3 highest values and sort alphabetically

I have this table with thousands of rows:
id | Keyword | hits
1 cat 3
2 cats 5
3 castle 1
4 cream 2
5 car 12
. . .
I want to select the 3 rows with most hits and then order alphabetically.
So I want to return:
car
cat
cats
I have this:
SELECT keyword,hits FROM table ORDER BY hits DESC, keyword ASC LIMIT 3
but it only orders by hits and then if hits are equal it orders alphabetically.
Wrap your SELECT - that gets only the 3 rows with highest hits - as a subquery inside another query that orders by the keyword:
SELECT * --- or just: SELECT keyword
FROM
( SELECT keyword, hits
FROM table
ORDER BY hits DESC
, keyword ASC --- this is needed only in case of ties at 3rd place
LIMIT 3
) tmp
ORDER BY keyword
You could use a subquery (don't know about your mysql version).
SELECT *
FROM
(SELECT keyword, hits
FROM table
ORDER BY hits DESC
LIMIT 0,3) AS topThree
ORDER BY keyword ASC