Update field if the ORDER BY is 1 descending? - php

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.

Related

select mysql from a row limit and asc php pdo

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

mysql select rows with top max value

i have a table in mysql with columns: id , view , agree.
i have upload my table's image below:
i want to select 8 rows that greater than others in view column. my condition is agree = 1.
can you tell me how can i do it by mysql query or php.
Select * from table_name WHERE agree = 1 ORDER BY view desc LIMIT 8
Try this:
SELECT * from table
WHERE agree = 1
ORDER BY view DESC
LIMIT 8
use limit and order by
Select * from mytable
where aggree=1
ORDER BY view DESC
LIMIT 8
You can do this:
SELECT *
FROM yourTableName
WHERE agree = 1
ORDER BY view DESC
LIMIT 8
You have to use ORDER BY clause.
SELECT * FROM <TABLE_NAME> WHERE AGREE = 1 ORDER BY VIEW DESC LIMIT 8
Use ORDER BY for DESC or ASC sorting.
and For selection of 8 rows use Limit Data Selections From a MySQL Database
Select * from table_name WHERE agree = 1 ORDER BY view desc LIMIT 8
If you want to take the top 8 'view' values from the table the MySql query for that is:
SELECT * FROM tablename WHERE agree=1 ORDER BY view DESC LIMIT 8;
Note: Replace tablename with the actual name of your table

MySQL PHP - SELECT WHERE based on a different position

Is it possible to sort data based on an order value set on a certaid IDs?
I want to make the following:
ID - Position
1 - 3rd
2 - 4th
3 - 5th
4 - 6th
10 - 7th
11 - 8th
12 - 9th
13 - 10th
14 - 11th
83 - 2nd
84 - 1st
I’ve been trying to implement this one, but it doesn’t generate the correct order:
SELECT * FROM table_name WHERE id IN (1,2,3,4,5,6,7,81,82) ORDER BY id = 3 DESC, id = 4 DESC, id = 5 DESC, id = 6 DESC, id = 7 DESC, id = 8 DESC, id = 9 DESC, id = 1 DESC, id = 2 DESC
If I was unclear, please ask me for more info. Thank you in advance.
SELECT * FROM table_name WHERE id IN (1,2,3,4,5,6,7,81,82) ORDER BY CAST(SUBSTR(position, 1, CHAR_LENGTH(position) - 2) as unsigned)
Yes you can use order by FIELD , below is an example you can modify the way you want.
SELECT * FROM
table_name
WHERE id IN (1,2,3,4,5,6,7,81,82)
order by FIELD(id,3,4,5,1,2,6,7,81,82)
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_field
I think this is sufficient you dont need to add anything in this.
SELECT * FROM table_name WHERE id IN (1,2,3,4,5,6,7,81,82) ORDER BY id DESC
or you can use, if want in particular sequence of ID,
SELECT * FROM table_name WHERE id IN (1,2,3,4,5,6,7,81,82) order by FIELD(id,3,4,5,6,7,8,9,1,2)
or you can use ORDER BY CASE

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)

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

Categories