applying multiple limits - mysql

I have a table that goes as follows:
content_id | contact_count
1 23
2 4
3 89
I want to select the content_id with the highest contact_count from the last 25 rows of the table.
I've tried many different things such as:
select content_id from research_products_content
where contact_count=(select max(contact_count)
from research_products_content order by rating_total_id desc limit 25)
order by rating_total_id desc limit 1

In your example, limit 25 is applied after the result (max, which is a single row) is selected. Try this instead:
SELECT tmp.content_id FROM (
SELECT *
FROM research_products_content
ORDER BY rating_total_id DESC
LIMIT 25
) AS tmp
WHERE tmp.contact_count = (
SELECT max(tmp2.contact_count)
FROM (
SELECT *
FROM research_products_content
ORDER BY rating_total_id DESC
LIMIT 25
) AS tmp2
)
LIMIT 1

Since the column number will be the same use UNION
select content_id from research_products_content
where contact_count=(select max(contact_count)
from research_products_content order by rating_total_id desc limit 25)
UNION
select content_id from research_products_content
where contact_count=(select max(contact_count)
from research_products_content order by rating_total_id desc limit 1
You might want to implement caching along the way

Related

Double DESC with Double LIMIT

I need a mysql SELECT which it select the last 20 ids and it shows the 7 with the biggest clicks but from the last 20 ids the 7 biggest clicks..and not from all ids I tried with DESC and LIMIT but its not working..any idea??
SELECT * FROM search ORDER BY clicks DESC, id DESC LIMIT 20
Or
SELECT * FROM search ORDER BY id DESC LIMIT 20
SELECT * FROM search ORDER BY clicks DESC LIMIT 7
Or
SELECT * FROM search ORDER BY id DESC LIMIT 20 UNION SELECT * FROM search ORDER BY clicks DESC LIMIT 7
Try this
SELECT * FROM
(
SELECT * FROM search ORDER BY id DESC LIMIT 20
) SUB
ORDER BY clicks DESC LIMIT 7
EDIT:
What is SUB doing..?
SUB is the alias for the subquery SELECT * FROM search ORDER BY id DESC LIMIT 20. It could have different names which is up to you.
SELECT * FROM /*<=== Query 2 - Will be executed after Query 1 is run, thus will perform a search within the results returned by Query1*/
(
SELECT * FROM search ORDER BY id DESC LIMIT 20 /*<=== SUBQUERY - Query 1 - Will be executed first */
)
ORDER BY clicks DESC LIMIT 7
What we're going here is that; first we get a result set of most recent 20 rows from search with SELECT * FROM search ORDER BY id DESC LIMIT 20 SUB and then we run a SELECT query IN the result set returned by SUB.
Try this:
your query should be
SELECT * FROM search ORDER BY clicks DESC LIMIT 0, 20
or
SELECT * FROM search ORDER BY id DESC LIMIT 0, 20
Where ' 0 ' represent the starting position of your result and ' 20 ' represent the length of your result.

How do I select 5 random rows from the 20 most recent rows?

I want to select 5 random rows from a table but only from the 20 most recent rows. I know the 2 statements separately would be something like:
SELECT * FROM table ORDER BY RAND() LIMIT 5
SELECT * FROM table ORDER BY date DESC LIMIT 20
How would I combine these 2 statements so it will select 5 random rows from the 20 most recent rows? Thanks.
Use an nested select
SELECT foo.* FROM (SELECT * FROM table ORDER BY date DESC LIMIT 20 ) as foo
ORDER BY RAND() LIMIT 5
Simply nest them:
SELECT * FROM (
SELECT * FROM table ORDER BY date DESC LIMIT 20
) ORDER BY RAND() LIMIT 5
Look up subqueries!
SELECT d.* FROM (SELECT * FROM table ORDER BY date DESC LIMIT 20) as d ORDER BY RAND() LIMIT 5;

How to select from a selection?

I'm creating a commenting system, which will have 2 top comments.
How can I select the latest 20 rows, and then from that selection, select the top 2 rows (likes-dislikes)? I can do it with a PHP loop, but it would not be as efficient. Currently I am just selecting the top 2 from the all the comments, but the two top comments never change, since people just up-vote those ones:
SELECT * FROM pagecomments WHERE page_id='$pageid' ORDER BY likes-dislikes DESC LIMIT 2
EDIT: The table is ordered by the the column "id", which is auto_increment. page_id is the page on the site. Sorry.
Nest your query:
SELECT *
FROM (
SELECT *
FROM pagecomments
WHERE page_id='$pageid'
ORDER BY date DESC
LIMIT 20
) t
ORDER BY likes-dislikes DESC
LIMIT 2
Order not only by LIkes, first order by date entered or timestamp. By ordering by date, you assure that you`ll get the latest 20 posts or comments.
SELECT * FROM pagecomments WHERE page_id='$pageid' ORDER by date_entered desc ,
likes-dislikes DESC limit 2
Since your id column is set to auto_increment, use it in a subquery:
select *, likes-dislikes
from (
select *
from pagecomments
where page_id='$pageid'
order by id desc
limit 20
) t
order by likes-dislikes desc
limit 2
Condensed SQL Fiddle Demo

MySQL fetch 10 randomly rows where 3 predefined rows needs to be part of the results and be at the beginning

I have a table with ID's from 1 to 20 and I need to fetch 10 rows (random), but 3 of the 10 rows are predefined and needs to be at the beginning of the result list - In one MySQL statement:
This works, but the production table contains over 500K rows:
SELECT id
FROM tableName
WHERE id IN (1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)
ORDER BY FIELD(id, 5,6,7) DESC, RAND()
LIMIT 10
I would need something like this:
SELECT id
FROM tableName
WHERE id IN (5,6,7,*)
ORDER BY FIELD(id, 5,6,7) DESC, RAND()
LIMIT 10
... what would be the right syntax?
You can use statement like this:
SELECT
id
FROM
(
SELECT
id,
IF(id IN (5,6,7),1,0) AS priority
FROM tableName
) t
ORDER BY priority DESC, RAND()
LIMIT 10
If UNION ALL is allowed, you could try something like this:
select id from tablename where id in (compulsary_1, compulsary_2, compulsary_3)
order by id desc
union all
select id from tablename where id not in (compulsary_1, compulsary_2, compulsary_3)
order by rand() limit 7
Playing around... I found this and also works:
SELECT id
FROM tableName
WHERE id IN (5,6,7) OR id > 0
ORDER BY FIELD(id,5,6,7) DESC, RAND()
LIMIT 10

Need Select top 10 people SQL

I need SQL query for MySQL to select top 10 people with most followers
my table
id | user_id | follow_id
1 3 6
2 3 7
3 4 6
4 5 6
5 7 3
6 9 7
From example user with id 6 have 3 time followed , 7->2 and 3->1, so TOP 10 will be
user with id 6,7,3 ...
SELECT `follow_id`, COUNT(1) AS `followers`
FROM `tbl`
GROUP BY `follow_id`
ORDER BY COUNT(1) DESC
LIMIT 10;
You want to use MySQL GROUP BY aggregation funciton
SELECT user_id, COUNT(follow_id) AS total_followers
FROM users
GROUP BY follow_id
ORDER BY total_followers LIMIT 10;
SELECT follow_id,count(id) AS cnt FROM table
GROUP BY follow_id ORDER BY cnt DESC LIMIT 10
You can use
SELECT user_id , COUNT(id) AS count FROM tbl GROUP BY follow_id ORDER BY count DESC LIMIT 10;
You need to group the results by the follow_id and then count how many results are in this group and sort this by the number of results per group in a descending order and then define you want to limit it to only 10 results which can be done by using LIMIT 0,10
The following query works perfectly in MySQL 5
SELECT follow_id, COUNT(follow_id) AS nr
FROM test.testtable
GROUP BY follow_id
ORDER BY nr DESC
LIMIT 0,10
Try some thing like this:
select follow_id
from myTable
group by follow_id
order by count(user_id)
Limit 10
SELECT follow_id,
COUNT(user_id) AS number_of_followers
FROM table
GROUP BY follow_id
ORDER BY number_of_followers DESC
LIMIT 10;