简体   繁体   English

mysql desc对我不起作用

[英]mysql desc doesnt work for me

it should make a list and at the top the highest percentage.. but it doesnt https://gyazo.com/ecde864ef09115b8b119eba8a39ecd68 here the picture when i run it 它应该列出一个列表,并在顶部的百分比最高..但它不是https://gyazo.com/ecde864ef09115b8b119eba8a39ecd68这里我运行它的图片

and here the code. 这里的代码。 What is wrong? 怎么了?

$sql = "SELECT band, concat(round(sum(punten) *100 /
(SELECT sum(punten) FROM bands)) , \"%\") AS percent
FROM bands
WHERE punten>0
GROUP BY band 
ORDER BY percent DESC;";

You percent column is a string, so they are sorted lexicographically (eg, 8 is treated as larger than 26, since "8" comes after "2"). 百分比列是一个字符串,因此它们按字典顺序排序(例如,8被视为大于26,因为“8”在“2”之后)。 Instead, you could sort on the numeric part, before the concatination. 相反,您可以在连接之前对数字部分进行排序。 This can be done with the current query, but it would be much more elegant to more the total punten to a subquery: 这可以用当前查询完成,但它会更优雅更总punten到一个子查询:

SELECT     band, CONCAT(ROUND(band_punten) * 100 / total_punten), '%') AS percent
FROM       (SELECT   band, SUM(punten) AS band_punten
            FROM     bands
            WHERE    punten > 0
            GROUP BY band) a
CROSS JOIN (SELECT SUM(punten) AS total_punten FROM bands) b
ORDER BY   band_punten DESC

Use the underlying value. 使用基础值。 Converting a number to a string and then back to a number is awkward. 将数字转换为字符串然后返回到数字是很尴尬的。 So: 所以:

SELECT band,
       concat(round(sum(punten) * 100 / sump) , \"%\") AS percent
FROM bands CROSS JOIN
     (SELECT sum(punten) as sump FROM bands) x
WHERE punten > 0
GROUP BY band 
ORDER BY sum(punten) DESC;

I also moved the subquery to the FROM clause. 我还将子查询移动到FROM子句。 This is often more efficient. 这通常更有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM