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
Related
I have a table like below
ID
1
2
3
4
5
I want to select ID 2 then ASC LIMIT 3. I want to get 2,3,4.
My select goes.
SELECT * FROM TABLEID WHERE ID = 2 AND status = 'unuse' ORDER BY ID ASC LIMIT 3
But I only get 1 record I am expecting 3 row to be returned base on the LIMIT 3
I am expecting 3 row to be returned base on the LIMIT 3
You are expecting wrong, because LIMIT can not create records that aren’t there to begin with. You have only one record with ID=2, so a WHERE clause selecting those records of course only returns this one.
You want WHERE ID >= 2 to first select all records that have an id 2 or greater, and then limit that selection to 3 records only.
select * from TABLED limit 1,4
1 is offset start point
4 is upto count point
SELECT * FROM TABLEID WHERE ID BETWEEN 2 AND 4
OR exclude the id
SELECT * FROM TABLEID WHERE ID >=2 status = 'unuse' AND id <> 1 ORDER BY ID ASC LIMIT 3
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;
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");
Is there a possible way to do this:
I want to update a field 'rank' according to the ORDER BY place.
For example: (pseudocode)
If id order by place = 1 then update rank field to place were id=get id
rank place id
1 1 5 PC
2 2 8 MAC
is this possible?
Something like this?
UPDATE tbl_name
SET rank = 1
WHERE id = (
SELECT id
WHERE condition
ORDER BY place DESC
LIMIT 1
)
Or from your comment (I think MySQL http://dev.mysql.com/doc/refman/5.0/en/update.html):
UPDATE tbl_name
SET rank = 10
WHERE id = 9
ORDER BY wins DESC
LIMIT 1
You can always do a SELECT to check if these are the records you wish to UPDATE as well:
SELECT *
FROM tbl_name
WHERE id = (
SELECT id
WHERE condition
ORDER BY place DESC
LIMIT 1
)
OR
SELECT *
FROM tbl_name
WHERE id = 9
ORDER BY wins DESC
LIMIT 1
Some RDBMS have a ROWNUM pseudocolumn in queries you can use for this.
You did not specify what database you are using, for example Oracle has this.
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