dongpi3237 2015-12-03 20:44
浏览 17
已采纳

从6个表构建一个mysql查询以减少php的使用

First, I don't know if this is the right exchange. I've been working on a system for requesting feedback in a score from 1 to 5 to people around you, but I'm having trouble building the query for displaying the result. Another dev has already made the tables, so I have to work with this. I have this mysql-database.

These are the simplified tables, so you get the idea. These are static:

users
user_id || user_name || user_email

questions
question_id || theme_id || question

themes
theme_id || theme_title

And these are the ones where the users can add records. A user selects a theme and sends requests to other users.

sentlists
list_id || created_by_user_id || theme_id

requests
request_id || to_user_id || sent_list_id || answered

responses
response_id || request_id || rating 

How can I build a query (if at all possible) that, when given a list_id, results in a result like this:

user_name || question name 1 || question name 2 || question name 3 || question name 4
Michael || 2 || 3 || 1 || 4

I've been breaking my head over this for the past couple hours, and I can't come up with a good solution. I could fall back to PHP, but that would result in multiple queries for one result.

  • 写回答

2条回答 默认 最新

  • dsvjw20866 2015-12-03 20:59
    关注

    If my cross tab assumption is correct, you are better off assembling the final results in code. In which case, it is probably unlikely you'll need multiple queries; the obvious JOINs with a helpful ORDER BY (such as ORDER BY user_name, question_name, answer) should make the code portion relatively simple.

    You cannot vary result fields in a single query. You can use code to create queries dynamically, but that often ends up more work than it is worth.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启