MySQL - How to select random after select top? - php

I have a query like this:
SELECT Id, Name, image, price, view FROM estore.product ORDER BY view DESC LIMIT 9
and I want select random 5 records in that query. I tried but this code doesn't work:
SELECT Id, Name, Image, Price, View FROM (
SELECT Id, Name, Image, Price, View FROM estore.product ORDER BY View DESC LIMIT 9)
ORDER BY RAND() LIMIT 5
How can I do? Thanks for watching?

A subquery must be named. Try:
LIMIT 9) as SubQueryAlias ORDER BY RAND()
^^^^^^^^^^^^^^^^^^

You may want to go and read this thread
Multiple rows alternative for RAND()
If your table is quite large (and it probably can end up quite large being a product table) the rand() limit is quite a slow query

SELECT Id, Name, Image, Price, View FROM estore.product ORDER BY RAND() LIMIT 5
(Using subquery for order and limit the same selection is madness...)

Related

How to execute another query in a running query after a specific number of records using php?

I searched a lot but didn't found the exact result that I need. I have a project of question and blog website. There are two tables question_table and blog_table, now I want to execute both in two different queries and ORDER BY date on one page as newsfeed. I'm sorry if I didn't explain well.
Attached Image for detail description (result must like this)
My sql query "SELECT * FROM blog_table WHERE status=1 ORDER BY posted_date DESC"
and same for question_table
"SELECT * FROM question_table WHERE status=1 ORDER BY posted_date DESC"
Please help me to findout the exact result in the image bellow.
How can I run this query:
select posted_at,user_id,post_title,post_data from blog_table
UNION
select asked_at,user_id,question_title,question_slug from question_table
order by date desc
Your ORDER BY only applies to the question_table query, not the whole thing. You need to put the UNION in a subquery so you can order the entire result.
SELECT *
FROM (
select posted_at AS date, user_id, post_title AS title, post_data AS content
from blog_table
UNION
select asked_at AS date, user_id, question_title AS title, question_slug AS content
from question_table
) AS x
order by date desc

Get random top 3 record from database : php-mysql

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.

Order results by results from a different table in one query

Is it possible to have something like this in one query,
Count how many likes a certain ID has in image_likes and then order results from images descending by how many likes they have.
SELECT
*
FROM
`images`
ORDER BY
(SELECT COUNT(`id`) FROM `image_likes` WHERE `image_id`=images.`id`) ASC
(I have of course made up field names, but this format should work)
If possible, you might want to change the way the system works so that you can just read the total likes from a field name rather than doing a subselect.
Untested
select imageid, count(imageid) from image_likes
Group by imageid
Order by Count(imageid) desc
select * from (SELECT *,(SELECT COUNT(*) as count from image_likes il WHERE ID = i.ID)
FROM images) tbl ORDER BY COUNT
untested

Php MySQL Most Liked Query for last 100 Records

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

Selecting random entry from MySQL Database

How can I select a single random entry from a MySQL database using PHP?
I want to select the Author, AuthorText, and Date?
SELECT Author, AuthorText, Date FROM table ORDER BY RAND() LIMIT 1
Take a look to this interesting article:
“Do not use ORDER BY RAND()” or “How to get random rows from table?”
ORDER BY rand() LIMIT 1
will sort all the rows in the table, which can be extremely slow.
Better solution : say your table has the usual primary key auto-increment field, generate a rendom number between min(id) and max(id) and select the closest id.
It will not be as random as a "true" random selection, because a id after a large hole of deleted ids will have a higher probability of being chosen. But it will take 50 µs instead of 2 seconds if your table is large...
SET #t = (SELECT FLOOR(a + (b-a)*rand()) FROM (SELECT min(id) as a, max(id) as b FROM table)
SELECT * FROM table WHERE id>#t ORDER BY id LIMIT 1;
You can order by a random & restrict to 1 row as follows:
select
author, authortext, date
from bookstable
order by rand()
limit 1

Categories