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");
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'm just starting with MySQL so I would like to know how can I select only 5 random rows in the last 50 entries of my database? I hope you understand my question.
I'm using PDO and what I have now is this:
$otherChoiseRig = $bdd->query("SELECT * FROM articulos WHERE cat = '$ArtCat' ORDER BY RAND() ");
$otherChoiseRig2 = $otherChoiseRig->fetchAll(PDO::FETCH_ASSOC);
Then I use a PHP foreach loop...
Thank you
The challenge is determining the last 50 entries. Assuming you have an auto-incremented id, you can do:
SELECT a.*
FROM (SELECT a.*
FROM articulos a
WHERE cat = '$ArtCat'
ORDER BY id DESC
LIMIT 50
) a
ORDER BY RAND()
LIMIT 5;
The key idea is the subquery to get the last 50 entries, and then the final query to get the 5 random rows. The subquery needs to specify how you identify the last 50.
$otherChoiseRig = $bdd->query("SELECT * FROM articulos WHERE cat = '$ArtCat' ORDER BY RAND() LIMIT 5 ");
$otherChoiseRig2 = $otherChoiseRig->fetchAll(PDO::FETCH_ASSOC);
just add limit
i assume in your table you have some date column so we can get last 50
SELECT * FROM (SELECT * FROM articulos WHERE cat = '$ArtCat' ORDER BY created_tiem desc limit 50 ) t order by RAND() limit 5;
This is My Table:
#id# #cpc#
100 10
87 9
101 9
4 6
188 5
it's sorted DESC according to 'cpc' column.
I Want to extract the rows one by one without referring to id.. such as you can see it.
SELECT * FROM table ORDER BY cpc DESC
first result is with the id 100
next one is with id 101 and cpc 9 not 87.. as the id is only increasing.. so it selects wrong rows not as what i want.
You can do something like this in php:
$sql = mysql_query("SELECT * FROM yourTable ORDER BY cpc DESC");
while(($row = mysql_fetch_array($sql)){
$id = $row['id'];
$cpc = $row['cpc'];
Select them using limit and offset:
SELECT *
FROM table
ORDER BY cpc DESC
LIMIT 1 OFFSET 0;
Then:
LIMIT 1 OFFSET 1
LIMIT 1 OFFSET 2
and so on.
You need to edit your ordering.
SELECT *
FROM table
ORDER BY cpc DESC,
Id ASC
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.
This is my table structure
MyTable
ID[P.K][auto increment] TopicID UID Comment
Now i want to get the last 20 comment for a TopicID but it should be sorted in ascending order !
[Just like Facebook by default shows last 20 comment only]
I am looking for an optimized version, i can do this with 2/3 query and php sort array, but looking for some better alternative
Sample Result with data
MyTable
ID TopicID UID Comment
1 1 10 AAAA
2 1 11 BBBB
3 1 10 CCCC
4 1 10 dddd
5 1 11 EEEE
6 1 10 FFFF
I want to get the last 3 result for a TopicID, the result should be
4 1 10 dddd
5 1 11 EEEE
6 1 10 FFFF
and not
6 1 10 FFFF
5 1 11 EEEE
4 1 10 dddd
First, select last 20 entries. Then sort them in ascending order. You can easily do this in a single query (with subquery):
select * from (
select * from your_table order by id desc limit 20
) tmp order by tmp.id asc
SELECT *
FROM (
SELECT *
FROM mytable
WHERE topicid = $mytopicid
ORDER BY
id DESC
LIMIT 20
) q
ORDER BY
id
or, more efficiently,
(
SELECT *
FROM mytable
WHERE topicid = $mytopicid
ORDER BY
id DESC
LIMIT 20
)
ORDER BY
id
This should be the shortest expression to do the job:
(select * from your_table order by id desc limit 20) order by id;
SELECT * FROM
(SELECT * FROM MyTable
ORDER BY ID DESC
LIMIT 20) ilv
ORDER BY ID;
I don't really understand??
What's wrong with a simple SELECT * FROM MyTable WHERE TopicID = 1 ORDER BY ID ASC LIMIT 20?
By the way, if you're showing the latest entered ones (ie. most recent), you'll want DESC (Descending), not ASC. Also, using the ID is very unreliable, you should have a DATETIME column which stores when the comment was entered.
EDIT: binaryLV's answer will do this correctly using a subquery. It's the same query as mine, DESC'd, and then resorted by ID.
You need to add a CommentDate Column and everytime you INSERT a comment use NOW() or GETDATE() then use this select:
SELECT Comment FROM MyTable WHERE TopicID=#ID ORDER BY CommentDate DESC, TopicID ASC LIMIT 20
Assuming that ID is auto_increment, which would allow you to use it as a pseudo-date field,
SELECT * FROM MyTable
ORDER BY ID DESC
LIMIT 20
You can try this
SELECT * FROM(
SELECT TOP 20 * FROM TableName
ORDER BY Id DESC
)
Naushad ORDER BY Naushad.Id