在mysql中选择非零列 [英] select non zero columns in mysql
问题描述
我有一种情况,我只需要显示非零列
id C1 C2 C3 C4
1 0 1 0 2
2 0 1 0 5
3 0 9 0 3
我希望输出如下
id C2 C4
1 1 2
2 1 5
3 9 3
SQL查询总是产生先前已知的列.因此,您无法在纯SQL中完成您想做的事情. (而且不能仅使用SQL来执行某些操作通常表明数据库设计不佳.但是有时候SQL只是关于如何获取数据,而不是如何显示数据.)
您可以做的是在一个查询中获取列的总和,然后动态地(手动或在使用Java,PHP或使用任何编程语言的应用程序中)构建一个新查询,该查询仅选择非零列. /p>
I have a scenario where I need to display only non zero columns
id C1 C2 C3 C4
1 0 1 0 2
2 0 1 0 5
3 0 9 0 3
i want the output as below
id C2 C4
1 1 2
2 1 5
3 9 3
An SQL query always results in before-known columns. So you cannot do what you want to do in pure SQL. (And not being able to do something with SQL only is often a sign of a poor database design. Sometimes however it's that SQL is about how to get data, not how to display it.)
What you can do is get the columns' sums in one query, then build a new query dynamically (manually or within an app with Java, PHP or whatever programming language is used) that only selects the non-zero columns.
这篇关于在mysql中选择非零列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!