what im tryng to do is select the last 5 order by viewtime and from that 5 selected to make a rand and to give me 1
example if i select viewlast: 56789, 56788, 56787, 56786, 56785 to give me rand of this 5 numbers 1, lets say this 56788 or this 56786
Update
$Last_Video = $db->fetch_all("select VID, thumb FROM video WHERE title LIKE '%" . $Channel['name'] . "%' ORDER BY viewtime DESC limit 5");
this was the command what i use before
Take the last 5 and then order by RAND():
$query = '
SELECT VID, thumb
FROM video
WHERE id IN (
SELECT VID
FROM video
WHERE title LIKE "%'.$Channel['name'].'%"
ORDER BY viewtime DESC
LIMIT 5)
ORDER BY RAND()
LIMIT 1
';
It might help you:
SELECT *
FROM (
SELECT * FROM <#TABLE> ORDER BY viewtime DESC LIMIT 5
) AS TBL
ORDER BY rand() LIMIT 1
Change <#TABLE> with table name.
Related
I have this line:
$query = mysql_query("SELECT * FROM livechat WHERE type='public' ORDER BY id ASC LIMIT 15") ;
And this is for chat, however ASC takes only first ID comments, so it shows only 15 old comments (id1, id2 and so on). If I use DESC instead of ASC, it shows new comments, but in a bad way - newest at the top, since this is a chat, newest comments must be at the bottom.
Try creating a temporary table that contains the last 15 results, and then ordering from that table.
select * from (
select * from livechat where type='public' order by id desc limit 15
) tmp order by tmp.id asc
try like this:
$query = mysql_query("SELECT *
FROM (
SELECT *
FROM livechat
WHERE type='public'
ORDER BY id DESC LIMIT 15
) t
order by t.id") ;
i want to get the last 6 rows but skip the last one in result:
SELECT * FROM stats_follow ORDER BY id DESC LIMIT 6
How can i make this?
Try this:
SELECT *
FROM (SELECT *
FROM TableName
ORDER BY FieldName DESC
LIMIT 5) sub
ORDER BY FieldName ASC
Try this:
SELECT * FROM stats_follow ORDER BY id DESC LIMIT 1,6
this is the code what im using
$Last_Video = $db->fetch_all('
SELECT VID, thumb
FROM video
WHERE VID IN (
SELECT VID
FROM video
WHERE title LIKE "%'.$Channel['name'].'%"
ORDER BY viewtime DESC
LIMIT 5)
ORDER BY RAND()
LIMIT 1
');
This is the error what give me
Message: Error during SQL execution: SELECT VID, thumb FROM video WHERE VID IN ( SELECT VID FROM video WHERE title LIKE "%funny%" ORDER BY viewtime DESC LIMIT 5) ORDER BY RAND() LIMIT 1<br />
MySQL Error: This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'<br />
MySQL Errno: 1235
how i can fix this problem ? its other way to make it ... so i dont get the error ...
Instead of using IN, you can use JOIN
SELECT v.VID, v.thumb
FROM video AS v
INNER JOIN
(SELECT VID
FROM video
WHERE title LIKE "%'.$Channel['name'].'%"
ORDER BY viewtime DESC
LIMIT 5) as v2
ON v.VID = v2.VID
ORDER BY RAND()
LIMIT 1
You can use below to bypass this error.
$Last_Video = $db->fetch_all('
SELECT VID, thumb
FROM video
WHERE VID IN (select * from (
SELECT VID
FROM video
WHERE title LIKE "%'.$Channel['name'].'%"
ORDER BY viewtime DESC
LIMIT 5) temp_tab)
ORDER BY RAND()
LIMIT 1
');
You don't need a subquery here. Try this:
SELECT VID, thumb
FROM video
WHERE title LIKE "%'.$Channel['name'].'%"
ORDER BY RAND() DESC
LIMIT 1
In MySQL 5.0.26 and later, you will get an error:
MySQL does not support LIMIT in subqueries for certain subquery operators:
Reference.
add this is your in condition
(SELECT * FROM (
SELECT * FROM table ORDER BY id DESC LIMIT 50
) sub
ORDER BY id ASC)
Why you cant use simple: ?
SELECT v.VID, v.thumb
FROM video as v
WHERE title LIKE "%'.$Channel['name'].'%"
ORDER BY viewtime DESC
LIMIT 5
what for subqueries here?
mysql is disabled read it ORACLE
DELETE FROM wall_orders WHERE order_id IN (
SELECT order_id FROM (SELECT order_id, COUNT(orders_products_id) as cnt FROM wall_orders_products GROUP BY order_id ORDER BY cnt DESC LIMIT 1000) y1 WHERE cnt > 170 LIMIT 1000)
235 - This version of MariaDB doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
Simple SQL and not possible
I am trying to display all records with field_featured = 1 to start at the top of my listing ... and all the others to display at RAND()
So...
SELECT * FROM myTable
If (field_featured = 1 then ORDER BY field_featured) OTHERWISE (ORDER BY RAND() )
How can I do this?
Try this:
ORDER BY (field_fieatured=1) DESC, RAND()
I have a table like that I want to take the 20 most hit singers and order them (these 20 singers) alphabeticly.
id name hit
----------------
1 Beyonce 2540
2 Eminem 1432
3 Pink 1642
4 Shakira 1234
.
.
For example I use this code
$query = mysql_query("SELECT * FROM pm_categories ORDER BY hit DESC limit 20");
I take the 20 with the most hits, but I want to also order them alphabeticly.
How can I do that?
This should do it:
SELECT *
FROM pm_categories
WHERE id IN (
SELECT id
FROM pm_categories
ORDER BY hit DESC
LIMIT 20
) ORDER BY name
You need to query based on the hits separately, and then use the ids of the top 20 to query and sort by name.
Since you have an older version of mysql, you will need to do a join instead.
SELECT cat.*
FROM (
SELECT id
FROM pm_categories
ORDER BY hit DESC
LIMIT 20
) top_hits
JOIN pm_categories cat ON top_hits.id = cat.id
ORDER BY cat.name
Try
$query = mysql_query("SELECT * FROM pm_categories ORDER BY hit DESC, name ASC limit 20");