MySQL 数据透视表
这几天,我一直在努力寻找这个问题的答案。虽然我还没有找到答案,但这个网站不断出现在我的搜索结果中,所以我想我应该尝试一下。顺便说一句,很棒的帖子格式选项。
我有一个像这样的表:
user_id | form_id | question_id | data_label | data_value
1 1 1 firstName Joe
1 1 2 lastName Smith
1 1 3 phone 5554443333
2 1 1 firstName Sally
2 1 2 lastName Jones
2 1 3 phone 3334445555
我想把它变成:
user_id | firstName | lastName | phone
1 Joe Smith 5554443333
2 Sally Jones 3334445555
我可以找到如何做到这一点的示例。我不太了解它们,但它们就在那里。当这个单一的表保存从具有任意可能数量的字段的各种表单输入的数据时,我的独特问题就出现了。因此,我可能有一个表:
user_id | form_id | question_id | data_label | data_value
1 1 1 firstName Joe
1 1 2 lastName Smith
1 1 3 phone 5554443333
2 1 1 firstName Sally
2 1 2 lastName Jones
2 1 3 phone 3334445555
3 2 1 fav_color red
3 2 2 fav_animal eagle
4 2 1 fav_color blue
4 2 2 fav_animal dog
然后我将传入 form_id 作为参数,从该表单中收集按用户分组的所有记录(它们都应该具有具有不同值的相同标签),然后将该数据显示为:
。 ..当 form_id = 1 时,报告如下所示:
user_id | firstName | lastName | phone
1 Joe Smith 5554443333
2 Sally Jones 3334445555
...当 form_id = 2 时,报告如下所示:
user_id | fav_color | fav_animal
3 red eagle
4 blue dog
我是高级 SQL 编程和过程的新手,无法自行弄清楚如何执行此操作。我需要查询能够处理任何数量/类型的字段,而不必在每个表单的查询中输入确切的可能字段名称。请注意,上面的第一个查询有 4 个字段,而第二个查询有 3 个字段。因此解决方案需要以这种方式灵活。
SQL 也可以从编程语言生成,因此如果有帮助的话,该解决方案还有更多选项。
如果您没有足够的信息,请通知我。
Over the past few days, I have been trying to find an answer to this problem. While I have not found an answer, this site keep appearing in my search results so I thought I would give it a try. Awesome formatting options for posts BTW.
I have a table like:
user_id | form_id | question_id | data_label | data_value
1 1 1 firstName Joe
1 1 2 lastName Smith
1 1 3 phone 5554443333
2 1 1 firstName Sally
2 1 2 lastName Jones
2 1 3 phone 3334445555
that I want to turn into:
user_id | firstName | lastName | phone
1 Joe Smith 5554443333
2 Sally Jones 3334445555
I can find example of how to do this. I don't understand them very well but they are out there. My unique problem comes into effect when this single table holds data entered from a variety of forms with any possible number of fields. So, I might have a table with:
user_id | form_id | question_id | data_label | data_value
1 1 1 firstName Joe
1 1 2 lastName Smith
1 1 3 phone 5554443333
2 1 1 firstName Sally
2 1 2 lastName Jones
2 1 3 phone 3334445555
3 2 1 fav_color red
3 2 2 fav_animal eagle
4 2 1 fav_color blue
4 2 2 fav_animal dog
I will then pass in the form_id as a parameter, gather all records from that form grouped by users (they should all have the same labels with different values), and then display that data as:
...when form_id = 1 the report looks like:
user_id | firstName | lastName | phone
1 Joe Smith 5554443333
2 Sally Jones 3334445555
...when form_id = 2 the report looks like:
user_id | fav_color | fav_animal
3 red eagle
4 blue dog
I am new to advanced SQL programming and procedures and am not able to figure out how to do this on my own. I need the query to be able to handle any number/type of fields without having to enter the exact possible field names into the query for each form. Notice the first query immediately above has four fields while the second has 3. So the solution needs to be flexible in this manner.
The SQL can also be generated from a programming language so there are more options with that solution if that helps.
Please inform me if you do not have enough information.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你可以尝试(未经测试)类似的东西
You could try (untested) something like