MYSQL hide field data if value in another field is set

霸气de小男生 提交于 2020-01-03 05:10:25

问题


Got a table like:

art.    type    price
a        b       1
a        c       2

would it be possible with a select to show filtered content as:

art.    type    price
a        b       
a        c       2

so that if the type is "b" don't show price data?

select art, type, price from x 
where type="b" hide price

回答1:


Logic of this sort probably best belongs in the presentation, rather than database, layer of your application. However, it is nevertheless possible using either MySQL's IF() function or its CASE expression—for example:

SELECT art, type, IF(type='b',NULL,price) price FROM x;

See it on sqlfiddle.



来源:https://stackoverflow.com/questions/18659065/mysql-hide-field-data-if-value-in-another-field-is-set

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!