How to Put condition in order by sql Query - mysql

This is my table name :- xyztable and in this table i have 3 column
A:- id
B:- Name
c:- Order_number
i have a sample data like
Id Name order_nmber
1 A 1
2 K 0
3 B 6
4 c 3
5 P 0
6 d 5
7 e 2
8 O 0
I wrote the query is
select * from xyztable order by order_number ASC;
that give me the result is
Id Name order_nmber
2 K 0
5 P 0
8 O 0
1 A 1
7 e 2
4 c 3
6 d 5
3 B 6
But i want the result like :-
Id Name order_nmber
1 A 1
7 e 2
4 c 3
6 d 5
3 B 6
2 K 0
5 P 0
8 O 0
So how can i get this in single query....Please help!!!!

You could do like below:
select * from xyztable order by order_number = 0, order_number ASC;

select * from xyztable
order by case when order_number=0 then 1 else 0 end ASC, order_number ASC;

Try something like this:
SELECT * FROM xyztable ORDER BY order_number = 0, order_number;

Related

Counting unique pairs, but applying that count to all of its entries in SQL (not removing duplicates)

Let's say I have a table:
ID A B
10 0 0
11 0 0
12 0 1
13 0 1
14 1 1
15 1 1
16 1 1
And I want my table output to be:
ID A B A_B_COUNT
10 0 0 2
11 0 0 2
12 0 1 2
13 0 1 2
14 1 1 3
15 1 1 3
16 1 1 3
but with the code I have here my output looks like this
SELECT ID, COUNT(*) AS A_B_COUNT
FROM table
GROUP BY A, B
ID A B A_B_COUNT
10 0 0 2
12 0 1 2
14 1 1 3
Any way I can create an sql query to is like my top table vs the one I make currently
Using: 10.5.5-MariaDB
Use window functions:
select t.*, count(*) over (partition by a, b) as a_b_count
from t;
You can do:
select e.A, e.B, c.cnt as A_B_COUNT
from entries e
join (
select A, B, count(*) as cnt
from entries
group by A, B
) as c
where e.A=c.A and e.B=c.B
See db-fiddle.

Mysql selecting rows

I have a database scheme with a table like this:
id --- username --- sex
1 A 1
2 D 2
3 F 1
4 G 2
5 H 1
6 x 1
7 r 1
I want to select only 2 males and lets say 4 females, male is 1 and female is 2. How would we achieve that in one mysql query and if I have more var's to select by ?
(select * from your_table where sex = 1 limit 2)
union all
(select * from your_table where sex = 2 limit 4)

Write a sql to pivot a table

I would like to create something like this
Input Data table
customer_id fee_name amount
1 a 1
1 b 3.25
1 c 1.75
1 d 2
1 e 0
2 a 1
2 b 3.25
2 c 1.75
2 d 2
2 e 0
3 a 1
3 b 3.25
3 c 1.75
3 d 3.5
3 e 0
4 a 1
4 b 3.25
4 c 1.75
4 d 3.5
4 f 1
5 a 1
5 b 3.25
5 c 1.75
5 d 3.5
5 f 1
See, you have to create groups for all fee_name and amount. So there would be 3 groups here. 1 resembling the schedule for customer 1 and 2, second group resembling customer 3 and the third group resembling schedule for customers 4 and 5. name this groups also
Output 1 should look like
group a b c d e f
1 1 3.25 1.75 2 0 0
2 1 3.25 1.75 3.5 0 0
3 1 3.25 1.75 3.5 0 1
Second output table should map these groups to customer id.
So second output should look like
customer id group
1 1
2 1
3 2
4 3
5 3
Any help is highly appreciated.
Thanks
SELECT x.*
, y.customer_id
, CASE WHEN #prev = x.fee_schedule THEN #i:=#i ELSE #i:=#i+1 END i
, #prev:=x.fee_schedule
FROM
( SELECT DISTINCT GROUP_CONCAT(amount ORDER BY fee_name) fee_schedule FROM my_table GROUP BY customer_id ) x
JOIN
( SELECT customer_id, GROUP_CONCAT(amount ORDER BY fee_name) fee_schedule FROM my_table GROUP BY customer_id ) y
ON y.fee_schedule = x.fee_schedule
JOIN
( SELECT #prev:=null, #i:=0 ) vars
ORDER
BY y.fee_schedule
, y.customer_id;

How to Update a Field From The Same Data in MySQL

id product main_image
--- ---------- -----------
1 1 0
2 1 0
3 1 0
4 2 0
5 2 0
6 2 0
7 3 0
8 3 0
9 3 0
10 3 0
I want to use mysql query to make table field datas (for main_image) like this
id product main_image
--- ---------- -----------
1 1 1
2 1 0
3 1 0
4 2 1
5 2 0
6 2 0
7 3 1
8 3 0
9 3 0
10 3 0
how can I do it? I want to set a main image for products.
I have about 3 millions record, I tried php but its too slow
You can use a subquery to select the smaller id form the table:
UPDATE myTable
SET main_image = 1
WHERE id IN
(SELECT MIN(temp.id)
FROM (SELECT * FROM myTable) AS temp
GROUP BY temp.product)
This...
UPDATE my_table x
JOIN
( SELECT product
, MIN(id) min_id
FROM my_table
GROUP
BY product
) y
ON y.product = x.product
AND y.min_id = x.id
SET x.main_image = 1;
... or just this ...
UPDATE my_table x
JOIN
( SELECT MIN(id) min_id
FROM my_table
GROUP
BY product
) y
ON y.min_id = x.id
SET x.main_image = 1;

Min Max Value in coloumn but with a certain condition

I have a MySql table with 3 coluomn : Nip, Bidang and Total.
I want to count min and max value of total with the same bidang. but i don't want to count min and max value for all coloumns.
Sample data:
NIP Bidang Total
1 A 10
2 A 5
3 A 1
4 B 4
5 B 7
6 C 8
7 C 9
And the result column:
MIN
1
1
1
4
4
8
8
Simply do this:
SELECT * FROM
(
(SELECT Bidang FROM TableName) T1 LEFT OUTER JOIN
(SELECT bidang, MIN(Total) MinVal, MAX(Total) MaxVal
FROM TableName
GROUP BY Bidang) T2 ON T1.Bidang=T2.Bidang
)
Result:
BIDANG MINVAL MAXVAL
A 1 10
A 1 10
A 1 10
B 4 7
B 4 7
C 8 9
C 8 9
See result in SQL Fiddle.
EDIT
To see the Total column also, add Total to first query.
SELECT * FROM
(
(SELECT NIP,Bidang,Total FROM TableName) T1 LEFT OUTER JOIN
(SELECT bidang, MIN(total) MinVal, MAX(Total) MaxVal
FROM TableName
GROUP BY Bidang) T2 ON T1.Bidang=T2.Bidang
)
Result:
NIP BIDANG TOTAL MINVAL MAXVAL
1 A 10 1 10
2 A 5 1 10
3 A 1 1 10
4 B 4 4 7
5 B 7 4 7
6 C 8 8 9
7 C 9 8 9
See result in SQL Fiddle.
Select bidang, min(total) MyMin, Max(total) myMax
From tableName
group by bidang
NIP bidang total
1 A 10
2 A 5
3 A 1
4 B 4
5 B 7
6 C 8
7 C 9
should return
A 1 10
B 4 7
C 8 9