I need take from MYSQL 5 values that taked from top 10 values randomly?
Exmple:
I'v 100 posts in my db and I want take 5 of them that have views number in top 10 posts. How can I do it by only one command in mysql?
SELECT * FROM
(SELECT * FROM posts order by id DESC limit 0,10) as p1
ORDER BY rand() limit 0,5
Please change fields according your database structure
Related
I have got this MYSQL
SELECT * FROM chat WHERE To_='$NameId' OR From_='$NameId' ORDER BY `DATE` DESC LIMIT 0,50
This code returns me data in this order
1 newest
2 newest
3 newest
4 newest
5 newest
But i want data to be returned in this way
5 newest
4 newest
3 newest
2 newest
1 newest
How can i do that?
Use this :
SELECT * FROM (select * from chat where To_='$NameId' OR From_='$NameId' ORDER BY DATE DESC LIMIT 0,50) sub ORDER BY DATE ASC
As I understand you want to change direction of records after getting first 50 records.
You can use sub-query feature of MySQL. Assume that result of your query is new, shiny table. So write SQL for desired result using this table (actually it is data set). (In our case we rename it as results)
SELECT * FROM (select * from chat where To_='$NameId' OR From_='$NameId' ORDER BY DATE DESC LIMIT 0,50) as results ORDER BY results.DATE ASC
Also if you have "count" of results you can do same thing using single-level query. Think about that;
SELECT * FROM chat WHERE To_='$NameId' OR From_='$NameId' ORDER BY `DATE` ASC LIMIT ***MyCount***,50
MySQL will not force you to rename your sub-query results but renaming
them results in more readable SQL codes.
I want to create a random image banner which fetches the image data from a MySQL database.
And I only want to fetch the latest three records, randomly.
How can I fetch the the 3 most recent records, in a random order?
I am the following query:
SELECT * FROM bottom_advt WHERE bottom_advt_page_name='News' ORDER BY RAND() LIMIT 1
but it's not working.
You said 3 records. So try:
SELECT * FROM bottom_advt WHERE bottom_advt_page_name='News' ORDER BY RAND() LIMIT 3
You're limiting your results to one with LIMIT 1. Change it to LIMIT 3 to get three results.
Try this if you want one of the last three records:
SELECT * FROM bottom_advt WHERE bottom_advt_page_name='News' AND id+3>last_insert_id() ORDER BY RAND() LIMIT 1
You could use this:
SELECT *
FROM bottom_advt
WHERE ID IN (SELECT * FROM (
SELECT id
FROM bottom_advt
WHERE bottom_advt_page_name='News'
ORDER BY id DESC
LIMIT 3) last)
ORDER BY RAND()
LIMIT 1
Subquery will return the last 3 id that have bottom_advt_page_name='News', and the outer query will randomly select one of those.
Please see fiddle here.
I am working on a featured page that lists records in different categories.
5 Newest, 5 Least Viewed, 5 Most Viewed..
That part is not difficult:
Newest: SELECT TOP 5 * ORDER BY ID_Record DESC
Least: SELECT * FROM tbl_Name WHERE ORDER BY Hits_Field LIMIT 5
Most: SELECT * FROM tbl_Name WHERE ORDER BY Hits_Field DESC LIMIT 5
Here is my question..
Because the newest records are possibly the least viewed they could feasibly show up in both queries. I want to eliminate the 5 newest records from consideration.
How do I write a SQL Statement like this:
SELECT * FROM tbl_Name
WHERE (NOT THE 5 NEWEST ID_Record BUT ALL OTHERS STILL IN CONSIDERATION)
ORDER BY Hits_Field LIMIT 5
I know there is a NOT IN consideration, but I am new to this and need help writing a nested statement for this.
May be this could work:
"SELECT * from table_name where Id_Record not in (SELECT Id_Record from table_name order by Hits_Field LIMIT 5) order by Hits_Field LIMIT` 5"
Try
SELECT *
FROM `tbl_Name`
ORDER BY `Hits_Field`
LIMIT 5,5
LIMIT actually can have two parameters: an offset and the amount of records. So if you want to drop the first 5 records and the select the next 5, use LIMIT 5,5.
You can offset
SELECT * FROM tbl_Name
ORDER BY Hits_Field LIMIT 5,5
Use Limit parameters.
1) To eliminate Newest 5 records
SELECT * FROM tbl_Name ORDER BY Hits_Field ASC LIMIT 5 [Parameter to display from **5** th record], 1000[Parameter to display up to 1000 record];
2) To eliminate Least-viewed(Oldest) 5 records
SELECT * FROM tbl_Name ORDER BY Hits_Field DESC LIMIT 5 [Parameter to display from **5** th record], 1000[Parameter to display up to 1000 record];
Hope This Will Help.
The logic would be
Grab the 10 least viewed (to have enough records to eliminate 5),
remove any that also show up in the newest (up to 5, but could be less),
then limit that quantity to 5
Try EXCEPT or MINUS, something like
SELECT * FROM
(
SELECT * FROM tbl_Name WHERE ORDER BY Hits_Field LIMIT 10
EXCEPT
SELECT TOP 5 * ORDER BY ID_Record DESC
)
LIMIT 5
I am using this query:
SELECT * from likes GROUP BY url ORDER BY count(*) DESC LIMIT 6
to fetch most liked record from my table 'likes'. It is working perfect for fetching most liked content of all time.
But now to I want to select the 6 most liked record from the last 100 records.
What will be the query for it ?
SELECT * FROM (select * from likes order by date desc limit 100) xx
Group by URL order by count(*) limit 6
Obtain the primary keys of the last 100 entries and narrow your query to it. Probably extremely easy if you have auto-increment keys.
SELECT * from likes
GROUP BY url
ORDER BY count(*) DESC
WHERE ID > MAX(ID)-100
LIMIT 6
I have a table called 'topics' in which all topics are saved. I want to select the latest 5 rows from that table, show them on one page, then select THE OTHER FIVE latest ones and show them on the other page.
I know how to echo all the topic names in a while loop, but the problem here is making mysql select 5 rows, then the other five for another page, not the same ones again. How to achieve this?
SELECT
*
FROM tablename
ORDER BY id DESC
LIMIT 0, 5
on the another page:
LIMIT 5, 5
$start = 0;
$count= 5;
$query = "select *
from topics
order by date desc
limit $start, $count"
First 5:
SELECT * FROM your_table ORDER BY any_order_criteria LIMIT 0,5
Second 5:
SELECT * FROM your_table ORDER BY any_order_criteria LIMIT 5,5
What i got from your problem is that you want to enable Pagination on your page.
This is a wonderful article on Pagination using PHP and MySQL.
http://www.tonymarston.net/php-mysql/pagination.html