How to select from a selection? - mysql

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

Related

How to get data from mysql db from nth row to nth row?

I am using this query but it is not working in mysql
SELECT TOP 1 * FROM (SELECT TOP 5 * FROM ads ORDER BY id DESC) ads ORDER BY id DESC
You can use ORDER BY ... LIMIT {[offset,] row_count | row_count OFFSET offset} (lets say you want to get 5 records from 10th - 10,11,12,13,14):
SELECT * FROM ads
ORDER BY id DESC
LIMIT 10,5
Although I assume you don't want to get them sorted by id but rather by views or similar criteria where ORDER BY views DESC would take a place (don't forget to to add index on views count).
The 'TOP' does not function on MySQL. You can edit the query in the following manner to get the job done
SELECT * FROM (SELECT * FROM ads ORDER BY id DESC LIMIT 5) ads ORDER BY id DESC LIMIT 5

Select last N rows from MySQL

I want to select last 50 rows from MySQL database within column named id which is primary key. Goal is that the rows should be sorted by id in ASC order, that’s why this query isn’t working
SELECT
*
FROM
`table`
ORDER BY id DESC
LIMIT 50;
Also it’s remarkable that rows could be manipulated (deleted) and that’s why following query isn’t working either
SELECT
*
FROM
`table`
WHERE
id > ((SELECT
MAX(id)
FROM
chat) - 50)
ORDER BY id ASC;
Question: How is it possible to retrieve last N rows from MySQL database that can be manipulated and be in ASC order ?
You can do it with a sub-query:
SELECT * FROM
(
SELECT * FROM table ORDER BY id DESC LIMIT 50
) AS sub
ORDER BY id ASC;
This will select the last 50 rows from table, and then order them in ascending order.
SELECT * FROM table ORDER BY id DESC LIMIT 50
save resources make one query, there is no need to make nested queries
SELECT * FROM table ORDER BY id DESC, datechat DESC LIMIT 50
If you have a date field that is storing the date (and time) on which the chat was sent or any field that is filled with incrementally (order by DESC) or de-incrementally (order by ASC) data per row put it as second column on which the data should be ordered.
That's what worked for me!!!! Hope it will help!!!!
Use it to retrieve last n rows from mysql
Select * from tbl order by id desc limit 10;
use limit according to N value.
if anyone need this
you can change this into
SELECT
*
FROM
`table`
WHERE
id > ((SELECT
MAX(id)
FROM
chat) - 50)
ORDER BY id ASC;
into
SELECT
*
FROM
`table`
WHERE
id > (SELECT MAX(id)- 50 FROM chat)
ORDER BY id ASC;
select * from Table ORDER BY id LIMIT 30
Notes:
* id should be unique.
* You can control the numbers of rows returned by replacing the 30 in the query

MYSQL Select random from two rows

Can someone help me with this query.
I have this table for sample:
FILE Table
UID
file
uploaded_on_date
view_count
What I want to consider is the top 30 most recent uploaded file and top 30 most viewed file then randomly select from them and limit by 10.
I am new to this mysql complex queries. A sample query would be good and i will be able to understand it.
Thanks.
SELECT
*
FROM
(
SELECT
*
FROM
tablefile
ORDER BY
uploaded_on_date DESC
LIMIT 30
UNION SELECT
*
FROM
tablefile
ORDER BY
view_count DESC
LIMIT 30
)
ORDER BY
RAND()
LIMIT 10;
select * from (
select * from table order by upload_on_date desc limit 30
union
select * from table order by view_count desc limit 30) t
order by rand() limit 10

Select specific rows MySQL

How can I select specific rows in a MySQL Table? So instead of selecting ... ORDER BY date can I do something like SELECT * FROM Posts FROM(5) TO(10) ORDER BY date DESC where I would be selecting Posts 5 to 10 in the descending order?
Your question is a little ambiguous. There are two possibilities:
You want only part of the results to be retrieved from the database, for example from the 5th one to the 10th one:
SELECT * FROM `posts` ORDER BY `date` DESC LIMIT 6 OFFSET 4
which will skip the 4 first results and given you next 6 results (beginning with 5th from the original set and ending with 10th from the original set).
You want results with specific IDs between 5 (inclusive) and 10 (inclusive):
SELECT * FROM `posts` WHERE `id` BETWEEN 5 AND 10 ORDER BY `date` DESC
or (which can be useful if the list of IDs is not the whole range):
SELECT * FROM `posts` WHERE `id` IN (5,6,7,8,9,10) ORDER BY `date` DESC
but this last example is only to show you different way of picking "specific" rows.
use limit:
SELECT * FROM Posts ORDER BY date DESC LIMIT 5, 5
http://php.about.com/od/mysqlcommands/g/Limit_sql.htm
You can use limit operator in mysql to do this. Ex: SELECT * FROM posts LIMIT 5 , 5;
Alternative Syntax which is more readable in my opinion:
SELECT * FROM Posts ORDER BY date DESC LIMIT 5 OFFSET 5
The first number indicates the number of results returned, the second one defines the offset from where to begin with.
If you use the LIMIT 5,5 it's the other way round.
You can use the limit:
SELECT * FROM tabel ORDER BY date DESC LIMIT 5,5
(5th till 10th record based on descending date).
SELECT * FROM Posts ORDER BY date DESC limit 5, 10

Mysql select first then limit

How can i select the newest 5 rows from mysql (newest by id for example) then order them asc or desc ??
the problem is i have 1,2,3,4,5,6,7 , i want to select the newest 2 for example (6,7) then order them asc or desc (6,7 or 7,6) , but if i use the orderby then the limit it would be (1,2) or (7,6) , is there anyway to do it from ONE sql statement ?
thank you
You may use something like
SELECT * FROM TABLE WHERE ID IN (SELECT ID FROM TABLE WHERE X ORDER BY ID LIMIT 2) ORDER BY ID DESC
I didn't get if you wanted the newest 5 or 2, I use 2 here. But here is something I just wrote without testing.
SELECT * FROM table WHERE id IN (SELECT id FROM table ORDER BY id DESC LIMIT 2) ORDER BY id ASC/DESC;
Hope this will help! :)
ok i found the solution , thank you all for your efforts and answers ,
i made 2 columns , ID and TSTAMP (for timestamp) , just made it like order by id asc , tstamp asc/desc ... limit x , so it orders always by the id then orders by the tstamp and limits the right thing .
and nested queries are a pain for the engine ...
thanks
Select * from TABLE order by id asc , time_stamp desc
Why can't you do something such as
SELECT *
FROM TABLE
WHERE (TABLE.primary_key IN (
SELECT TABLE.primary_key
FROM TABLE
ORDER BY TABLE.id ASC
LIMIT 2
))
ORDER BY TABLE.id DESC