order by clause on status in my sql - mysql

I am doing a sorting on a table to show the records with status having 3 first then status having 4 and then 1.
in current table this is the output
id status
1 3
2 4
3 4
4 3
5 1
now when i apply the query
select * from table order by model.status desc
the output is:
id status
2 4
3 4
1 3
4 3
5 1
what i want actually the below output. first with status 3 then with status 4 and then with status 1. How to achieve the below output
id status
1 3
4 3
2 4
3 4
5 1

instead of a magic 9999999 number, you can use ~0, which represents the maximum possible value for big int... should be enough ;)
order by (case when status = 1 then ~0 else status end)
other solution, without any magic number
order by status = 1 , status
which will sort by a "boolean" (0 or 1 in DB) first, then by status

You can use FIELD to do that easily on MySQL;
SELECT *
FROM model
ORDER BY FIELD(status, 3, 4, 1);
SQLfiddle here.

If FIELD is not working for you (perhaps due to older version of MySql) then try this. It will work
SELECT * FROM model
ORDER BY CASE status WHEN 3 THEN 1 WHEN 4 THEN 2 WHEN 1 THEN 3
END, id
Check it working on this link

Following should work
ORDER BY CASE status WHEN 3 THEN 1
WHEN 4 THEN 2
WHEN 1 THEN 3
END, id asc

Use SQL FIELD
SELECT * FROM `table` ORDER BY FIELD (status, 3, 4, 1);

$invoiceList = InvoicesModel::where('hotel_id', $hotel_id)
->where('status', 'PENDING')
->orWhere('status', 'COOKING_PROCESS')
->orderBy(
DB::raw('(CASE WHEN status = "COOKING_PROCESS"
THEN 1 WHEN status = "PENDING"
THEN 2 END)')
)->get();

Use "FIELD()" in order by clause if status values are 1,3 and 4 only.
select * from table order by FIELD( model.status 3,4,1)

Related

MySQL Group by consecutive values and count

I have a table that looks like this
id
1
2
4
5
6
10
11
So a bunch of consecutive values, an unknown number of absent fields and then other consecutive values.
What I am trying to achieve is to get
id
stint
1
0
2
0
4
1
5
1
6
1
10
2
11
2
By incrementing every time the number of the stint, which I can later use for summing over other columns.
Is it possible? Thanks
If your MySQL version support window function.
You can try to use LAG window function in subquery to get previous id column, then use SUM condition aggregate window function.
Query #1
SELECT Id,
SUM(id - n_Id > 1) OVER(ORDER BY id) stint
FROM (
SELECT *,LAG(id,1,id) OVER(ORDER BY id) n_Id
FROM T
) t1
Id
stint
1
0
2
0
4
1
5
1
6
1
10
2
11
2
View on DB Fiddle

How get the record count of having status all 1's

I'm saving person image in one table, one person will have multiple images, each image will have status 1 or 0, we need to get the record based on image status. if the image status of a person record is all 1's then we need to consider that record if any one of the status is 0 then we need to skip the record from the result
Original Result
id psqno status
1 1 1
2 1 1
3 1 0
4 2 1
5 2 1
6 3 1
7 3 0
8 4 1
9 4 1
Expected Result
id psqno status
1 2 1
2 4 1
I have tried the query like below:
select count(distinct psqno) as image_record from users where status = 1
I would like to get the count of result whose image having all status as 1.
You can use conditional aggregation to find groups for which no other status exists:
SELECT psqno
FROM t
GROUP BY psqno
HAVING COUNT(CASE WHEN status = 1 THEN 1 END) = COUNT(*) -- all rows have status = 1
Demo on db<>fiddle

Mysql results differs at evens from singles in same query

I have a strange error first time seing such results maybe someone else also found how to overpass such odd error.
I have a table structure like this :
|id|uid|test1|test2|rid|
1 1 - - 1
2 1 - - 1
3 2 - - 2
4 2 - - 2
5 3 - - 3
6 3 - - 3
7 4 - - 4
8 4 - - 4
----------------------------
as "-" are varchar data all other columns are integers
My query with the odd results is this :
SELECT COUNT(uid) AS COUNT,
id AS ID,
uid AS InqID,
test1 AS A,
test2 AS B
FROM test_table
WHERE rid = (X)
GROUP BY uid
ORDER BY id DESC
As results i get
if (X) = 1 or 3 it shows me the correct last id number (2 id for 1 and 6 id for 3)
but if (X) is 2 or 4 it shows me the first in row id number (3 id for 2 instead of 4 and 7 id for 4 instead of 8)
Can anyone tell me why i get the correct results only as singles in rid and not at even numbers of rid column or how at least this query will work as it has to ?
Thank you all in advance
Use MAX(id) instead of ORDER BY.
SELECT COUNT(uid) AS COUNT, MAX(id) AS ID, uid AS InqID, test1 AS A, test2 AS B
FROM test_table
WHERE rid = (X)
GROUP BY uid

how to write the sql

i have a table like
ID USER_ID type
1 1 2
2 1 1
3 3 3
4 3 1
5 6 2
6 6 3
and i want to get the sum of all user_id = 1 and user_id =3
and every type sum in user_id = 1 and user_id =3 ,it can be two sql
the result is
sum
4
type sum
1 2
2 1
3 1
The first function you needed to achieve the result is Count() not SUM() function
For first result
select count(*) as sum from user1 where user_id in(1,3)
For second result you need to select type also and it should be grouped
select type, count(*) as sum from user1 where user_id in(1,3)
group by type
Fiddle for second you can check the first one also there by taking the query
Next time post your effort. Guess you are naive to sql.

SQL Ordering Data from row "n+1" to "n-1"

I have a simple question. In MySQL, consider a row "n", how can we order rows by id (for example), but start from the row "n+1" and end to the row "n-1" ?
Thanks !
EDIT : I ommit to precise that I seek the query in MySQL.
From an answer below, here an example :
ID
---
1
2
3
4 <--N
5
6
I want Desired Results ordered as follows
5 <--N + 1
6
1
2
3 <--N - 1
So you mean. For a table
ID
---
1
2
3
4 <--N
5
6
You want Desired Results ordered as follows?
5 <--N + 1
6
1
2
3 <--N - 1
If so
SELECT ID
FROM T
WHERE ID <> 4
ORDER BY CASE WHEN ID > 4 THEN 0 ELSE 1 END, ID
Assuming table MyTable with integer column N:
SELECT *
from MyTable
where Id between N-1 and N+1
order by N desc
You're asking how to sort by descending ?
Just stick
ORDER BY col a , col b DESC;
at the end