【发布时间】:2017-02-07 07:22:38
【问题描述】:
我正在通过 Python (pymysql) 查询 MySQL 数据库 (fintech_16) 以获取列 (Trend) 的唯一值。当我运行以下查询时:
cursor.execute ("SELECT DISTINCT `Trend` FROM `fintech_16` ")
cursor.fetchall()
我得到了以下结果:
((u'Investments',),
(u'Expansion',),
(u'New Products',),
(u'Collaboration',),
(u'New Products,Investments',),
(u'New Products,Expansion',),
(u'Expansion,Investments',),
(u'New Products,Collaboration',),
(u'Regulations',),
(u'Investments,New Products',),
(u'Investments,Expansion',),
(u'Collaboration,Investments',),
(u'Expansion,New Products',),
(u'Collaboration,New Products',))
现在。由于某些 id 具有不止一种趋势,因此 DB 将它们计为单独的趋势。
我应该如何调整我的查询以仅获得 5 个趋势(投资、扩张、新产品、合作、法规)及其数量?
虽然这些只有 5 个,但我可以使用 LIKE %Investments% 手动获取计数,但我希望代码/查询能够做到这一点。
TIA
【问题讨论】:
-
您的预期输出是什么。编辑并将其添加到您上面的问题中。
-
GIGO。见规范化。