0
我构建了一个小型python API,它应该返回一个填充了所有必需键/值对的JSON。该键值刚刚从MySQL数据库查询,如:在MySQL结果中过滤NULL列
SELECT *
FROM flowers
WHERE favourite = 1
输出:
id color size favourite ...
---------------------------------------------
1234 yellow big 1 ...
1235 <null> small 1 ...
1236 blue <null> 1 ...
1237 <null> <null> 1 ...
执行该查询将返回一个数组,看起来与此类似:
[
{'id': 1234, 'color': "yellow", 'size': "big", 'favourite': 1, ... },
{'id': 1234, 'color': "<null>", 'size': "small", 'favourite': 1, ... },
{'id': 1234, 'color': "blue", 'size': "<null>", 'favourite': 1, ... },
{'id': 1234, 'color': "<null>", 'size': "<null>", 'favourite': 1, ... }
]
为了为了防止向客户端发送任何无用的条目,我想选择我正在选择的内容。我的目标是继续选择favourite=1
的每一行,但只是离开列,返回<null>
。所以我得到的结果等于:
[
{'id': 1234, 'color': "yellow", 'size': "big", 'favourite': 1, ... },
{'id': 1234, 'size': "small", 'favourite': 1, ... },
{'id': 1234, 'color': "blue", 'favourite': 1, ... },
{'id': 1234, 'favourite': 1, ... }
]
我对性能也很感兴趣。是否可以/值得通过MySQL查询达到我的目标,还是更好地选择<null>
结果,然后使用自定义python方法对它们进行过滤?
谢谢你回答我的问题。在我的情况下,我使用python,但原理是一样的。 – user3191334
糟糕,抱歉,我没有阅读标签。也许你应该把它转换成Python并发布你的代码,这样你就可以接受一个正确的答案。 – Barmar