MYSQL动态交叉表问题
这里是新手。
我正在使用 codeigniter 和 mysql 如何动态(名称数量可能会改变)将表从: 转换
+------+-------+-------+
| date | name | value |
+------+-------+-------+
| 06-01| A | 1 |
| 06-02| A | 2 |
| 06-02| B | 3 |
| 06-03| C | 4 |
+------+-------+-------+
为:
+------+---+---+---+
| date | A | B | C |
+------|---+---+---|
| 06-01| 1 | | |
| 06-02| 2 | 3 | |
| 06-03| | | 4 |
+------+---+---+---+
?
谢谢。
Newbie here.
I'm using codeigniter and mysql
How can I dynamically (number of names may change) convert table from:
+------+-------+-------+
| date | name | value |
+------+-------+-------+
| 06-01| A | 1 |
| 06-02| A | 2 |
| 06-02| B | 3 |
| 06-03| C | 4 |
+------+-------+-------+
To:
+------+---+---+---+
| date | A | B | C |
+------|---+---+---|
| 06-01| 1 | | |
| 06-02| 2 | 3 | |
| 06-03| | | 4 |
+------+---+---+---+
?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
像这样的东西应该有效。
您需要知道列名是什么,才能将每个 SUM 手动添加到 SQL 语句中,但如果可能发生很大变化,您可以使用 PHP 来完成此操作。
同样,如果您只想计算每个名称出现的次数,而不是
name
中值的总数,请将value
替换为1
。Something like this should work.
You need to know what your column names could be to add each of the SUMs manually into your SQL statement, but you could do this using PHP if it was likely to change a lot.
Likewise, replace
value
with1
if you just wanted a count of how many times each name appeared, rather than the total of the values inname
.