select mysql from a row limit and asc php pdo - php

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

Related

Select Next mysql row without refer to id

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

Ordering the SQL query in a particular order

Say i have a table Guest and it has column g_id : values 1 to 10.
Now i want the query to return me the g_id's neither in ascending order nor in descending..
but i want the 4th then 3rd and then 5th entry, in this particular order.
Also i want just the 4th 3rd and 5th entry.
say my entries have an id and a name . ;i.e. my table Guest has these two tables.
Now my table is as following.
1 A
2 B
3 C
4 D
5 E
6 F
7 G
8 H
9 I
10 J
Now i want just the entry with 4th 3rd and 5th g_id, and in this particular order.
How do i write the SQL query?
Thanks.
Select * from Guest ___________???
Kindly fill in the gaps.
You can use a CASE statement in your ORDER BY to use a fake column to sort on and a WHERE IN clause to only return the values you need.
SELECT *
FROM Guest
WHERE g_id IN (3, 4, 5)
ORDER BY
CASE WHEN g_id = 4 THEN 1
WHEN g_id = 3 THEN 2
WHEN g_id = 5 THEN 3
END
What is the order that deteremines whether something is 4th, 3rd or 5th? Without an ORDER BY clause, the data is returned in an indeterminate order by SQL. You cannot rely on the order that rows are entered or stored in the database table itself.
You can hard-code what you are asking like this:
select *
from Guest
order by case
when g_id = 4 then 1
when g_id = 3 then 2
when g_id = 5 then 3
else 4
end
One solution is the case statement:
select g_id from (
select g_id, case g_id
when 4 then 1
when 3 then 2
when 5 then 3
else 0
end virtcol
where virtcol != 0
order by virtcol
);
I'm not sure how set your ordering will be, but you can order by specifics:
ORDER BY
g_id = 4 DESC,
g_id = 3 DESC,
g_id = 5 DESC
You may be better off selecting the entries as they are and doing something like this in your php code:
$order = array('4 ', '3 ', '5 ');
$data = array();
while ($row = $result->fetch()) {
$data["$row->g_id "] = $row;
}
$data = array_merge(array_flip($order), $data);
I think that the answer mostly depends on the DBMS you are working on.
In Oracle the query below, even though inefficient, should work
select * from
(select * , rownum as order from guest order by id asc ) b
where b.order = 4
UNION
select * from
(select * , rownum as order from guest order by id asc ) b
where b.order = 3
UNION
select * from
(select * , rownum as order from guest order by id asc ) b
where b.order = 5
Not sure if something of more efficient is possible with a simple query,
i would use the monster above only and only if the table you are querying is very small.
You also have another option if the table is big and you have to extract only the first rows. In the case you described, I would retrieve the first 5 rows and then programmatically I would extract the rows in position 4,3,5.
you can extract the first 5 rows with this query in oracle
select * from guest order by id asc where rownum < 6
This query will get you the 3rd, 5th, and 4th items (limit 2, 1 means "retrieve starting with 3rd item, with total number retrieved = 1 records)
(select g_id from Guest limit 2,1)
UNION (select g_id from Guest limit 4,1
UNION (select g_id from Guest limit 3,1)

Update field if the ORDER BY is 1 descending?

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.

Select last 20 order by ascending - PHP/MySQL

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

mysql row counter

I have a mysql table. It has auto increment on the id. but I regularly delete rows so the numbers are all over the place. I need to get the last n rows out, but because of deletions, the common way of using the max of the autoincremented id column doesn't work well...
1 - Is their another way to get the bottom 50?
2 - Is their a way to get rows by actual row number? so if I have 4 rows labelled 1,2,3,4 delete row 2 then it will become 1,2,3 rather than 1,3,4?
SELECT ... ORDER BY id DESC LIMIT 50
SELECT *
FROM TABLE
ORDER BY id DESC
LIMIT 50
EDIT
To pick the last 50, but sort by id ASC
SELECT X.*
FROM ( SELECT *
FROM TABLE
ORDER BY id DESC
LIMIT 50
) X
ORDER BY X.id
1 - First get total row count like
SELECT COUNT(*) AS c FROM ...
then use
SELECT ..... LIMIT [start],[count]
2 - One idea is to use view , or procedure, but this is much more harder and may be used when there is no other way to avoid this
1 - Is their another way to get the bottom 50?
SELECT * FROM table_name ORDER BY record_id DESC LIMIT 50
2 - Is their a way to get rows by actual row number? so if I have 4 rows labelled 1,2,3,4 delete row 2 then it will become 1,2,3 rather than 1,3,4?
SELECT * FROM table_name
1 - Yes but it is ugly afaik, you do a
SELECT whateveryouwant FROM table ORDER BY yourprimarykey DESC LIMIT 50
the you fetch the rows into an array and reverse the array, in php :
$r = mysql_query('SELECT * FROM table ORDER BY primarykey DESC LIMIT 50');
$set = array();
while($row = mysql_fetch_assoc($r)) $set = $row;
$set = array_reverse($set);
foreach($set as $row) {
// display row ...
}
2 - You'll have to manage your primary key by yourself, its a bit risky ...

Categories