简体   繁体   English

如何在mysql中选择可选列?

[英]How to select optional columns in mysql?

I want to run a query on my MYSQL database. 我想在我的MYSQL数据库上运行查询。 Depending on what the values are, I want to select different fields. 根据值是多少,我想选择其他字段。

For example, if field 'e' is null, then I want to SELECT fields a,b,c,d. 例如,如果字段“ e”为空,那么我想选择字段a,b,c,d。 If field 'e' is not null, then I want so SELECT fields a,b,c,d,e. 如果字段“ e”不为null,那么我要选择字段a,b,c,d,e。

Something along the lines of 遵循以下原则

response = SELECT a,b,c,d (AND e IF e IS NOT NULL) FROM table1 WHERE id = 1;

The number of columns in a query result set cannot change. 查询结果集中的列数不能更改。 You can't return four columns on some rows and five columns on other rows from a given query. 给定查询不能在某些行上返回四列,而在其他行上返回五列。

You'll just have to write your application code with a condition to ignore the fifth column if it's null. 您只需编写带有条件的应用程序代码即可忽略第五列(如果为空)。

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

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