I have a table 'book_history' with a field 'status'
So there are three values for the field 'status' => 0,1,2
Now i want to query it in orderby - I know order by asc and order by desc is there.
But how i really want is
select * from book_history order by status 1,0,2
I checked with order by then also. But I was not able to make my query.
So the final output will be - first it should list the status=1, then status=0 and then status='2'
Any help.
Thanks,
Kimz
You can use FIELD() function
SELECT
*
FROM
book_history
ORDER BY FIELD(`status`, 1, 0, 2)
Use below query to force order in query:
select * from book_history
ORDER BY FIELD (status,1,0,2);
For more information if there is more values and you keep your values on top and rest values after that then you can use below query:
select * from book_history
ORDER BY FIELD (status,2,0,1) desc;
You can use ORDER BY FIELD:
SELECT * FROM book_history ORDER BY FIELD(status, 1,0,2)
Use a case expression to get an order key:
select *
from book_history
order by
case status
when 1 then 100 -- any small value would do here
when 0 then 200 -- any medium value would do here
when 2 then 300 -- any big value would do here
end;
Another alternative with UNION:
select * from book_history where status = 1
UNION
select * from book_history where status = 0
UNION
select * from book_history where status = 2
Related
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
i have a MySql table that consists of 2 basic things:
The id and a value.
To show that on my page, i need to select, for example, the last 100 rows on reversed order.
So imagine that someone is putting data on it:
Id, value
1, 10
2, 9
3, 21
4, 15
i need, to select the last "3" rows (LIMIT + ORDER Clause), but not like this: 4,3,2 but like this: 2,3,4.
I know how to do that on code, but maybe there is a simple solution for that on Mysql and i don`t know.
Thanks
My SQL Query is like this right now:
SELECT `Data`.`id`, `Data`.`log_id`, `Data`.`value`, `Data`.`created` FROM `control_panel`.`datas` AS `Data` WHERE `Data`.`id` > 1000 AND `Data`.`log_id` = (2) ORDER BY `Data`.`id` DESC LIMIT 100
You need to wrap the first ORDER BY in a subselect which will return a limited selection ordered in descending order, then you can order that result in the outer query in ascending order:
SELECT
a.*
FROM
(
SELECT id, value
FROM tbl
ORDER BY id DESC
LIMIT 3
) a
ORDER BY
a.id
One way to do this would be with a sub-select.
SELECT *
FROM (SELECT * FROM table_name ORDER BY id DESC LIMIT 3) tmp
ORDER BY id ASC
simply
SELECT t.*
(SELECT * FROM table_name
ORDER BY column_name DESC
LIMIT 0,3) t
ORDER BY t.column_name ASC
use DESC to descending order, ASC to increasing order
with this code I select the first 30 row of the table:
SELECT * FROM `table` LIMIT 0 , 30
But how to select the last 30, without changing the order?
It looks like everyone is missing this part:
But how to select the last 30, without changing the order?
First of all, clearly there is no order in the query provided. Assuming the order is ascending on some field this would be the query #DannyFox meant:
SELECT * FROM T
ORDER BY val
LIMIT 0 , 30
Now imagine we have simplified data, such as a, b, c, d, e and that we want only 3 rows instead of 30:
SELECT * FROM T
ORDER BY val
LIMIT 3
If this returns: a, b, c, d, e in each row, then he would expect to get c, d, e in that order. The query everyone is providing:
SELECT * FROM T
ORDER BY val desc
LIMIT 3
Would return e, d, c. The problem with this is that it's actually changing the original order, and the OP say he didn't want to change the order. So technically, the query that would result in c, d, e is:
select * from (
select * from t
order by val desc
limit 3
) s
order by val
Which actually changes the order twice, getting the original order back.
Since you are trying to avoid ordering, then the solution would be to apply it twice.
SELECT *
FROM (
SELECT *
FROM `table_name`
ORDER BY `column_name` DESC -- maybe id?
LIMIT 0, 30
) `table_aliase`
ORDER BY `column_name` ASC
First you need to specify an order:
SELECT * FROM table
ORDER BY some_id ASC -- ascending order
LIMIT 30
If that query returns the first 30 columns, this one will return the last 30:
SELECT * FROM table
ORDER BY some_id DESC -- descending order
LIMIT 30
If you have an auto incremental key/column, say id then here's an example
SELECT * FROM `table` ORDER BY id DESC LIMIT 0 , 30;
Maybe this will work: select * from table WHERE id > ((SELECT MAX(id) from table) - 30);
Nothing is said about an order, so you can not use ORDER BY.
There is a way to get records from a given point, but for that you need to know how many records there are, then use this counted value to provide the limits
SELECT COUNT(*) AS counted FROM table
SELECT * FROM table LIMIT (counted-30),30
Here's my method used with PHP:
$query = "SELECT * FROM table ORDER BY id DESC LIMIT 3";
$res = mysql_query($query);
$results = array();
while($row = mysql_fetch_assoc($res)){
$results = $row[field];
}
// Back to original order based from DB
$results = array_reverse(results);
I have a stupid question, I have this table :
id_product name value
1 price 10-20
1 type computer
2 price 20-30
3 price 100-200
and I want to select from this table GROUP BY id_product and ORDER BY value WHERE name='price'
how can i do this?
Thanks a lot
select * from table_name WHERE name='price'
GROUP BY id_product
ORDER BY value
In PHP
mysql_query("select * from table_name WHERE name='price'
GROUP BY id_product
ORDER BY value") or die(mysql_error());
SELECT *
FROM table_name
WHERE name = 'price'
GROUP BY id_product
ORDER BY value
You can see this for MySQL Select syntax
select * from table_name where name='price' Group By id_product ORDER By value;
right off the top of my head...
does it work?
SELECT * FROM `table` WHERE `table`.`name`='price' GROUP BY `table`.id_product ORDER BY `table`.`value` ASC
It depends on how you want to group items with the same id: which one do you want to be shown? In theory GROUP BY shouldn't be used with SELECT * (it does not work with non mysql databases) and should be associated with aggregate funcions such as COUNT(), MAX(), etc.
You might just need SELECT DISTINCT instead of GROUP BY.
This will select distinct id_product from table_name and order by value where name is "price". I think this will give the outcome you're after.
SELECT DISTINCT id_product
FROM table_name
ORDER BY (
SELECT t.value FROM table_name t
WHERE t.name='price'
AND t.id_product = table_name.id_product
)
How can I get the latest entry by the latest DATE field from a MySQL database using PHP?
The rows will not be in order of date, so I can't just take the first or last row.
You want the ORDER BY clause, and perhaps the LIMIT clause.
$query = 'SELECT * FROM `table` ORDER BY `date` DESC LIMIT 1';
SELECT * FROM [Table] ORDER BY [dateColumn] DESC
If you want only the first row:
In T-SQL:
SELECT TOP(1) * FROM [Table] ORDER BY [dateColumn] DESC
In MySQL:
SELECT * FROM `Table` ORDER BY `dateColumn` DESC LIMIT 1
You don't have a unique recordid or date revised field you could key in on? I always have at least an auto incrementing numeric field in addition to data created and date revised fields. Are you sure there is nothing you can key in on?
SELECT * FROM table ORDER BY recno DESC LIMIT 1;
or
SELECT * FROM table ORDER BY date_revised DESC LIMIT 1;
So the PHP call would be:
$result = mysql_query("SELECT * FROM table ORDER BY date_revised DESC LIMIT 1");
-- Nicholas
You can use a combination of the LIMIT and ORDER BY clauses.
For example:
SELECT * FROM entries ORDER BY timestamp DESC LIMIT 1