SELECT TOP N records by column value - mysql

For instance, in table id/article/view_counter I would like to select top 5 articles by view_counter.
I could SELECT *, order by view count and then take only first 5 when I loop through array but is there a way to do that directly in query?

I believe you want to limit your records
SELECT ID, Article, count(view_counter) from table group by 1,2 order by 3 limit 5
Not sure your table structure, if view_Counter is already aggregate, you would just take the count and the group by off...
SELECT * from table order by 3 limit 5
This is assuming the view counter is your third column
If not then you would use
SELECT * from table order by view_counter limit 5

Related

How can I select the row from a table whose 1 field is maximum or minimum?

In my table I have a field name rt_stars which includes integer anyone between 1 to 5. Suppose there are 5 rows having rt_stars 4,4,3,2,1 respectively. Here, 4 is the highest and 1 is the lowest. If I want to select the row with the maximum value 4 how can I do that? Since there are two 4 here the last one will be selected. Can I have query something like this?
SELECT * FROM ratings WHERE MAX(rt_stars) ORDER BY rt_id DESC LIMIT 1
I know this is wrong but that's how I want to select all the values from the rows if the rt_stars field has the maximum value in it. How can I achieve this kind of a query?
You can select one row using:
select r.*
from ratings r
order by rt_stars desc, rt_id desc
limit 1;
The problem with your query is that you cannot use max() in the where clause. Beyond that, you don't need aggregation at all -- just ordering the rows and then selecting the first one.
If you want all rows with the maximum stars you can do:
select *
from ratings
where rt_stars = (select max(rt_stars) from ratings)
If you just want one of them randomly you can do:
select *
from ratings
order by rt_stars desc
limit 1
I think the below SQL code will help.
SELECT * FROM ratings WHERE rt_stars = MAX(rt_stars) ORDER BY rt_id DESC;
Sorry the code modified below
SELECT * FROM ratings WHERE rt_stars = ( SELECT MAX( user_05_pk ) FROM ratings )
ORDER BY rt_id DESC;

Last 5 entries or more depending on where

I want to run a query where the last 5 entries are always returned, which is simple: SELECT * FROM table ORDER BY id DESC LIMIT 5. But I want to get more than 5 if a certain condition applies, which by itself would simply be something like SELECT * FROM table WHERE field > value. Is there a way to do this as a single query or do I have to run 2?
Maybe with UNION?. Like:
(SELECT * FROM table ORDER BY id DESC LIMIT 5)
UNION
(SELECT * FROM table WHERE field > value)

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

Possible to select last 100 records in MySQL, then select random 10 from those results in one query?

I need to display 10 related videos on a video page that come from the same category as that video. The problem is that there could possibly be hundreds of thousands of rows for each category so running RAND() is out of the question and I would prefer not to create a myisam table that matches my innodb table and then full text search for related.
I am not sure if my idea is possible, but I would like to select 100 of the latest rows for that category ordered by date, and then select only 10 from that set randomly.
Is this possible and could you point me in the right direction please?
I'm assuming you have a simple table with an identity named ID, and you can do something like:
SELECT *
FROM (
SELECT ID, Name, VideoFile
FROM VideoTable
ORDER BY ID DESC
LIMIT 100
) Derived
ORDER BY RAND()
LIMIT 10
select * from
(select * from table ORDER BY DESC LIMIT 100)
ORDER BY rand()
LIMIT 10

MySQL limit from descending order

Is it available to write a query to use same "LIMIT (from), (count)", but get result in backwards?
In example if I have 8 rows in the table and I want to get 5 rows in two steps I would:
first step query:
select * from table limit 0, 5
first step result:
first 5 rows;
second step query:
select * from table limit 5, 5
second step result:
last 3 rows;
But I want to get it vice versa. I mean from the first step I want last 3 rows and from the second I want 5 first rows. Thank you for your answer
No, you shouldn't do this. Without an ORDER BY clause you shouldn't rely on the order of the results being the same from query to query. It might work nicely during testing but the order is indeterminate and could break later. Use an order by.
SELECT * FROM table1 ORDER BY id LIMIT 5
By the way, another way of getting the last 3 rows is to reverse the order and select the first three rows:
SELECT * FROM table1 ORDER BY id DESC LIMIT 3
This will always work even if the number of rows in the result set isn't always 8.
Let's say we have a table with a column time and you want the last 5 entries, but you want them returned to you in asc order, not desc, this is how you do it:
select * from ( select * from `table` order by `time` desc limit 5 ) t order by `time` asc
yes, you can swap these 2 queries
select * from table limit 5, 5
select * from table limit 0, 5
This way is comparatively more easy
SELECT doc_id,serial_number,status FROM date_time ORDER BY date_time DESC LIMIT 0,1;