mysql 数据库排序依据
mysql database order by
<table>
<tr>
<th>name</th>
<th>sum(value)</th>
</tr>
<tr>
<td>null</td>
<td>85</td>
</tr>
<tr>
<td>arun</td>
<td>74</td>
</tr>
<tr>
<td>dhin</td>
<td>55</td>
</tr>
</table>
我有一个像上面那样的 table 数据。我想根据 sum(value) desc 对项目进行排序,如果名称为空,则必须最后显示。如何在 mysql
中编写查询
我的查询是 SELECT p.class,p.buyref,p.no_of_mat_id,p.tot_cos from (select a.class,group_concat(distinct a.buyref SEPARATOR '","' ) as buyref,count(*) as no_of_mat_id,sum(tc) as tot_cos from (select a.class,a.buyref,sum(b.total_cost) as tc from (SELECT distinct a.class,b.buyref FROM material_master
a join rxml_reference
b on a.cat_id=b.cat_id where a.language='0161-1#LG-000001#1' and b.property='BUYER REFERENCE' and b.buyref<>'' 和 b.buyref in ("E268200006","E570500008","E330010072","1274266","1257657","1216933","1275795","1269085 ","1216931","1257656","1213833","5066725","1275794","5067252","1257658","1236240","1274249","1213824","1275797","5067664", "1236239","1248625","1275796","5066724","5065949","1213989","1216932","1257659","1227545","1280565","1280549","1213829","1264792 ","1280550","1280551","1216943","1249846","1210250","1282003","1248620","5065948","1213830","1216942","1210217","1210247", "1279855","1280567","1280553","1210292","1281546","1213846","1238253","1210323","1264791","5065610","1217304","1216973","1271479 ","1285240","1217303","5063937","5063934","1285241","1238255", "1280552","1280566","1292638","5075432","1292635","1293721","1258418","1290188","5071130","1292061","1258417","1292634","1292631 ","1292632","1292633","1292639","1292060","5071580","1293251","5075431","1292597","36066","1268083","1219900","49194", "46646","1272051","48743","1219891","49604","41693","1247650","1241850","41728","49588","1258736","44917","1219898" ","1266629","39660","1267486","1270445","1222352","1259901","36074","1272388","1275153","1240833","1237806","46645", "1219897","1271400","1272385","41066","36073","1244303","1241848","41730","1220612","45332","41565","49204","1244246 ","45329","1272384","1222919","1231947","1225515","1272052","1271399","49664","46118","1241847","49691","1219899", "1273498","52919","46350","49201","1272750","1237807","29632","1220800","36481","41709","1276924","40133","36062 ","36268","49684","1273492","1276923","36302","1281374","49603","1219892","49480")) a join final_transaction b on a.buyref=b.item 其中 b.accounting_date >= '2014-1-01 00:00:00' 和 b.accounting_date <= '2018-4-31 00:00:00' 和 b.supplier_id 在 ('GLOBL-ASP01-0000005443','GLOBL-ASP01-0000005443','GLOBL-NFT01-0000124166','GLOBL-ASP01-0000001239','APCPM-APCPM-A0012496','GLOBL-ASP01-0000001643','ACM_388437','ACM_11590352','ACM_5174231','ECM_18494') group by a.class,a.buyref)a group by a.class order by sum(tc) desc) p ORDER BY p.tot_cos desc
SELECT name,SUM(value)
FROM TABLE1
ORDER BY CASE WHEN name IS NULL THEN 1 ELSE 0 END,SUM(value) Desc
输出
name sum_value
trun 84
arun 74
dhin 55
(null) 85
演示 Link
<table>
<tr>
<th>name</th>
<th>sum(value)</th>
</tr>
<tr>
<td>null</td>
<td>85</td>
</tr>
<tr>
<td>arun</td>
<td>74</td>
</tr>
<tr>
<td>dhin</td>
<td>55</td>
</tr>
</table>
我有一个像上面那样的 table 数据。我想根据 sum(value) desc 对项目进行排序,如果名称为空,则必须最后显示。如何在 mysql
中编写查询我的查询是 SELECT p.class,p.buyref,p.no_of_mat_id,p.tot_cos from (select a.class,group_concat(distinct a.buyref SEPARATOR '","' ) as buyref,count(*) as no_of_mat_id,sum(tc) as tot_cos from (select a.class,a.buyref,sum(b.total_cost) as tc from (SELECT distinct a.class,b.buyref FROM material_master
a join rxml_reference
b on a.cat_id=b.cat_id where a.language='0161-1#LG-000001#1' and b.property='BUYER REFERENCE' and b.buyref<>'' 和 b.buyref in ("E268200006","E570500008","E330010072","1274266","1257657","1216933","1275795","1269085 ","1216931","1257656","1213833","5066725","1275794","5067252","1257658","1236240","1274249","1213824","1275797","5067664", "1236239","1248625","1275796","5066724","5065949","1213989","1216932","1257659","1227545","1280565","1280549","1213829","1264792 ","1280550","1280551","1216943","1249846","1210250","1282003","1248620","5065948","1213830","1216942","1210217","1210247", "1279855","1280567","1280553","1210292","1281546","1213846","1238253","1210323","1264791","5065610","1217304","1216973","1271479 ","1285240","1217303","5063937","5063934","1285241","1238255", "1280552","1280566","1292638","5075432","1292635","1293721","1258418","1290188","5071130","1292061","1258417","1292634","1292631 ","1292632","1292633","1292639","1292060","5071580","1293251","5075431","1292597","36066","1268083","1219900","49194", "46646","1272051","48743","1219891","49604","41693","1247650","1241850","41728","49588","1258736","44917","1219898" ","1266629","39660","1267486","1270445","1222352","1259901","36074","1272388","1275153","1240833","1237806","46645", "1219897","1271400","1272385","41066","36073","1244303","1241848","41730","1220612","45332","41565","49204","1244246 ","45329","1272384","1222919","1231947","1225515","1272052","1271399","49664","46118","1241847","49691","1219899", "1273498","52919","46350","49201","1272750","1237807","29632","1220800","36481","41709","1276924","40133","36062 ","36268","49684","1273492","1276923","36302","1281374","49603","1219892","49480")) a join final_transaction b on a.buyref=b.item 其中 b.accounting_date >= '2014-1-01 00:00:00' 和 b.accounting_date <= '2018-4-31 00:00:00' 和 b.supplier_id 在 ('GLOBL-ASP01-0000005443','GLOBL-ASP01-0000005443','GLOBL-NFT01-0000124166','GLOBL-ASP01-0000001239','APCPM-APCPM-A0012496','GLOBL-ASP01-0000001643','ACM_388437','ACM_11590352','ACM_5174231','ECM_18494') group by a.class,a.buyref)a group by a.class order by sum(tc) desc) p ORDER BY p.tot_cos desc
SELECT name,SUM(value)
FROM TABLE1
ORDER BY CASE WHEN name IS NULL THEN 1 ELSE 0 END,SUM(value) Desc
输出
name sum_value
trun 84
arun 74
dhin 55
(null) 85
演示 Link