MYSQL查询用于动态转换行到列标题

时间:2017-08-21 08:55:48

标签: mysql

+--------+----------+-----------+---------+
| hostid | itemname | itemvalue | quantity|    
+--------+----------+-----------+---------+
|   1    |    A     |    10     | 1       |
|   1    |    B     |     3     | 2       |
|   2    |    A     |     9     | 3       |
|   2    |    c     |    40     | 4       |
+--------+----------+-----------+---------+

如何查询数据库以返回如下内容:

+--------+-----------------+-----------------+-------------------+
| hostid |  A              |           B     |        C          |
|        |-------+---------+-------+---------+--------+----------+        
|        |itemval| quantity|itemval| quantity| itemval| quantity |
+--------+-------+---------+-------+---------+--------+----------+ 
|   1    |  10   |  1      |   3   |     2   |   nuLL |    NULL  |
|   2    |   9   |  3      |   NULL|   nULL  |   40   |    4     |
+--------+-------+---------+-------+---------+--------+----------+

任何人都可以帮我吗?

0 个答案:

没有答案