简体   繁体   English

使用php mysql查询选择具有最高值的5列

[英]select the 5 columns with highest values using php mysql query

I need 5 columns with highest values (values must be sum of each column) for report. 我需要5列具有最高值的列(值必须是每一列的总和)。 I have about 40 contury colunms something like england_turist, polad_turists, us_turists, india_turists... eng - 152(sum), pol - 280(sum), us - 54(sum), in - 36(sum)... and query needs to pick pol - 280 and eng - 152. So query need to sum every column and pick 5 columns with highest values. 我有大约40个contury社团,例如england_turist,polad_turists,us_turists,india_turists ... eng-152(sum),pol-280(sum),us-54(sum),in-36(sum)...和查询需要选择pol-280和eng-152。因此查询需要对每一列求和,并选择5个具有最高值的列。

I got result from here but how to create query 我从这里得到结果,但是如何创建查询

SELECT * FROM
(
     SELECT 'Bmw' AS vrstaautomobila, SUM(bmw) AS brautomobila FROM unos_korisnika
     UNION
     SELECT 'Mercedes' AS vrstaautomobila, SUM(mercedes) AS brautomobila FROM unos_korisnika
     UNION
     SELECT 'Opel' AS vrstaautomobila, SUM(opel) AS brautomobila FROM unos_korisnika
     UNION
     SELECT 'Nissan' AS vrstaautomobila, SUM(nissan) AS brautomobila FROM unos_korisnika
     UNION
     SELECT 'Peguoet' AS vrstaautomobila, SUM(peguoet) AS brautomobila FROM unos_korisnika
     UNION
     SELECT 'Volkswagen' AS vrstaautomobila, SUM(volkswagen) AS brautomobila FROM unos_korisnika
)
AS results
ORDER BY brautomobila DESC
LIMIT 5

Transpose the table (convert columns to rows) and then select the top values. 转置表(将列转换为行),然后选择顶部值。

select * from transposed_table order by value_column desc limit 5

you may use How to transpose mysql table rows into columns to transpose your data or may be this SQL - How to transpose? 您可以使用如何将mysql表行转置为列来转置数据,或者使用此SQL-如何转置?

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

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