简体   繁体   English

如何从当前的MySQL数据库查询和重新组织数据?

[英]How to Query & re-organize Data from current MySQL database?

I have the following database structure: 我具有以下数据库结构:

数据库结构

And the data stored inside is as follows: 里面存储的数据如下:

test_summary

test_details

However, I like to select data from this database in the following format: 但是,我希望以以下格式从该database中选择数据: 我想要的是

How can I use one single SELECT statement to achieve this? 如何使用单个SELECT语句来实现这一目标? If using one single SELECT statement cannot, how to achieve this by whatever sql code? 如果不能使用单个SELECT语句,如何通过任何sql代码实现呢? Can someone give me the implementation? 有人可以给我实施吗?

Can I use one single SELECT statement to achieve this? 我可以使用一个SELECT语句来实现吗?

Yes, you can. 是的你可以。 You will have to play a little with JOIN and GROUP BY statement, but it can be achieved. 您将不得不使用JOINGROUP BY语句,但是可以实现。

EDIT: 编辑:

Let's try multiple JOIN statements: 让我们尝试多个JOIN语句:

Select sum.name, x0.y_value, x10.y_value, x20.y_value
    from test_summary as sum
    join test_details as x0 on sum.id=x0.id and x0.x_value=0
    join test_details as x10 on sum.id=x10.id and x10.x_value=10
    join test_details as x20 on sum.id=x20.id and x20.x_value=20

Based on answers to multiple joins and join with where clause 基于多个联接的答案,并使用where子句 联接

The things you like called pivot table, and this is out of scope of relation data base system like mySQL. 您喜欢的东西称为数据透视表,这超出了关系数据库系统(如mySQL)的范围。 So there many special platforms for OLAP. 因此,有许多用于OLAP的特殊平台。 But if you do not need any comprehensive OLAP things, but produce some pivot tables using mySQL and PHP you do not need any special sql, but nedd to program such presentation by php, may be using some commercial frameworks like this or some opensource code 但是,如果你不通过PHP需要任何全面OLAP的东西,但生产使用MySQL和PHP,你不需要任何特殊的SQL一些数据透视表,但NEDD编程这样的表现,可能是使用一些商业框架,像这样或一些开源代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM