I have a table with a column to maintain the state of the record. i.e.
-----------------------------
| id | desc | state |
-----------------------------
| 1 | desc 1 | Complete |
| 2 | desc 2 | Open |
| ... | ... | ... |
-----------------------------
I want fetch the records in the order of 'Open' followed by 'Complete'. Can I get this done using one SQL query? If so, how should I write it?
Yes, you could do this with the ORDER BY statement and FIELD function:
SELECT * FROM table1 ORDER BY FIELD(state, 'Open', 'Complete')
Try something like this:
select *
from table_name
order by decode (state, 'Open', 1, 'Complete', 2, 3)
Related
I have a table with some columns and I want to order the selection by the column 'post_id'. The rows are like this: 'rgpost0', 'rgpost1','rcpost2', ...
How can I order the selection by the number at the end of the value of the column 'post_id' descending?
This code is not working: SELECT * FROM posts_info ORDER BY post_id+0 DESC
I don't want to change the column type to number. I just want to order by number at the end of the string.
You can use reverse() twice:
SELECT * FROM posts_info
ORDER BY reverse(reverse(post_id) + 0) + 0 DESC
For this table:
create table posts_info(id int, post_id varchar(100));
insert into posts_info(id, post_id) values
(1, 'bob52'),
(2, 'alice634'),
(3, 'john12'),
(4, 'mark7'),
(5, 'mary001');
Results:
| id | post_id |
| --- | -------- |
| 2 | alice634 |
| 1 | bob52 |
| 3 | john12 |
| 4 | mark7 |
| 5 | mary001 |
See the demo.
If you are using MySQL 8+ then we can use REGEXP_SUBSTR here:
SELECT *
FROM posts_info
ORDER BY
CAST(REGEXP_SUBSTR(post_id, '[0-9]+$') AS UNSIGNED) DESC,
post_id DESC;
I added a second sorting level in case two post_id happen to end in the same number. In this case, I order descending by the entire post_id.
Try this
SELECT * FROM posts_info ORDER BY CAST(SUBSTR(post_id, 7) AS UNSIGNED) DESC;
We have a DB called transaction. It has user_id, date, value and so on. I use pagination in my query also. I have thousands of record in my table which has user_id equal to 2 or other value. put the user_id = 2 at the very last page.
I want to sort the result like this:
sort the results by date but if the user_id= 2 , put all results associated with the user_id= 2 at the end.
to be more clear, I show you what I want in the below.
-------------------------------------
| ID | user_id | date | ......
-------------------------------------
| 1 | 10 | 2018-10-20 |
-------------------------------------
| 2 | 11 | 2018-10-21 |
-------------------------------------
| 3 | 2 | 2018-10-22 |
-------------------------------------
| 4 | 2 | 2018-10-23 |
the results have to be like this:
first: ID = 2, second: ID = 1, third: ID = 4, last: ID = 3
tip *:
I use field function but unfortunately in vain.
ORDER BY FIELD(user_id, 2) DESC, date DESC
You may try using a CASE expression in your ORDER BY clause:
SELECT *
FROM yourTable
ORDER BY
CASE WHEN user_id = 2 THEN 1 ELSE 0 END,
date DESC;
I'm not sure if you want each group sorted by date ascending or descending. If you want ascending date order, then remove the DESC keyword at the end of my query.
how to select 2 data each time from mysql database in reverse order. For example, in this table
|---------------------|------------------|
| id | category |
|---------------------|------------------|
| 1 | 1 |
|---------------------|------------------|
| 2 | 3 |
|---------------------|------------------|
| 3 | 1 |
|---------------------|------------------|
| 4 | 2 |
|---------------------|------------------|
| 5 | 1 |
|---------------------|------------------|
| 6 | 3 |
|---------------------|------------------|
| 7 | 1 |
|---------------------|------------------|
I want to select 2 data each time where category is 1 in reverse order.
For first time running of query, I will get data of id =7 and id =5. For second time running of query, I will get data of id =3 and id =1. How to form that query?
You can use different (by limit) queries for that:
select id from [your-table-name-here] where category=1 order by id desc limit 0, 2 --to get first two
select id from [your-table-name-here] where category=1 order by id desc limit 2, 2 --to get next two
...
select id from [your-table-name-here] where category=1 order by id desc limit x, x+2 --etc
You are going to want to use another language in addition to SQL to do this. Here is an example using PHP:
$query_string = "SELECT `id` FROM `<TABLE NAME>` WHERE `category` = 1 ORDER BY `id` DESC";
$result = mysqli_query($link, $query_string); //where $link stores the connection to your MySQL database
if ($result != null) {
$highest_id = mysqli_fetch_row($result)[0];
$second_row = mysqli_fetch_row($result);
if ($second_row != null) $second_highest_id = $second_row[0];
}
//etc
i have a table like this
i want to get the row of each table that have min responsetime
i have tried this query :
select tablename,
index1,
index2,
min(responsetime)
from tableconf
group by tablename
order by responsetime asc
but it doesn't give what i want
the output that i want is
+------------------+------------------+--------+--------------+
| tablename | index1 | index2 | responsetime |
+------------------+------------------+--------+--------------+
| salesorderheader | TotalDue | NULL | 6.1555 |
| salesterritory | Name | NULL | 11.66667 |
| store | BusinessEntityId | Name | 3.6222 |
| previous | previous | NULL | 5.03333 |
| NONE | NONE | NULL | 5.6 |
+------------------+------------------+--------+--------------+
what query i should use for get the output that i want
Select the minimum date per table name. Use an IN clause on these to get the rows:
select *
from tableconf
where (tablename, responsetime) in
(
select tablename, min(responsetime)
from tableconf
group by tablename
);
(Edited from previous answer)
I don't know if all SQL syntax accept a comma separated where parameter. Another option building off of the highest voted answer right now utilizes a join:
select *
from tableconf t
inner join (
select tablename, min(responsetime) min_rt
from tableconf t2
group by tablename
) t3 on t.tablename = t2.tablename and t.responsetime = t2.min_rt
I have a problem here trying to get one of my CASE WHEN statement to query each row for something called is_op as it's returning the same number for all rows. Here is the code:
SELECT `mid`, `message`, `created_at`,
CASE WHEN (SELECT `uid` FROM `bulletin_message` WHERE `bid` = 1 ORDER BY `mid` ASC LIMIT 1) = 5 THEN 1 ELSE 0 END AS `is_op`,
CASE WHEN `bulletin_message`.`uid` = 5 THEN 1 ELSE 0 END AS `is_me`
FROM`bulletin_message`
WHERE `bid` = 1
GROUP BY `mid`
ORDER BY `mid` ASC
As you can see I'm trying to select messages with the condition bid must equal to 1 and uid must equal to 5. While is_me returns the correct value for each row, is_op isn't reflecting the correct value for all the rows at all. It displays 1 at the result of the statement, rather than showing if a user is an OP or not based on the oldest value of mid or created_at. I don't think I am correctly querying each row like is_me statement.
This is all the data of the table:
mid = message uid; bid = bulletin/thread uid; uid = user uid
| mid | bid | uid | message | created_at |
---------------------------------------------------
| 3 | 1 | 5 | ... | ... |
| 5 | 1 | 6 | ... | ... |
| 6 | 2 | 7 | ... | ... |
| 9 | 1 | 5 | ... | ... |
| 10 | 1 | 7 | ... | ... |
| 11 | 1 | 6 | ... | ... |
What can be done to improve this line of code so that it can query each row? Thank you!
Edit: OP is Original Poster, sorry for not clarifying that! It's usually the person who post the first in each bid.
The problem is your subquery is based on a fixed predicate, ``bid= 1, so it is bound to return the same value for all rows.
Something like this would make more sense:
SELECT `mid`, `message`, `created_at`,
CASE WHEN (SELECT `uid`
FROM `bulletin_message` AS t2
WHERE t1.`bid` = t2.`bid`
ORDER BY `mid` ASC LIMIT 1) = t1.`uid`
THEN 1
ELSE 0
END AS `is_op`
FROM`bulletin_message` AS t1
ORDER BY `mid` ASC
The subquery is correlated using bid field: it returns the OP of the current thread.