duanque2413 2013-09-17 10:05
浏览 59

mysql查询在数组结果中形成特定的数组

I have a table user with normal user detail fields as name, country , city etc. Further on submit this form I have a set of question about user interest in particular technology. The user can answer zero , one or more than one answer. I am saving these answer in user_answers table with column - id , user_id , question_id and answer.

user table structure
id - int - auto increment
name - varchar
country - varchar
city - varchar

user_answer table structure
id - int - auto increment
user_id - int
question_id - int
answer - varchar

Now I have a task of export all user to excel sheet with the answer given by the user. For which I am trying to get user data with its given answer using minimum sql query. The data should be in array format below :

$data = Array ( [0] => Array ( [id] => 1 , [name] => abc , [city] => xyz , 
        answer => Array( [0] => Array ( [question_id] => 1 , [answer] => aaa)
                         [1] => Array ( [question_id] => 3 , [answer] => bbb)
                         [2] => Array ( [question_id] => 4 , [answer] => ccc)
                        ))
                [1] => Array ( [id] => 2 , [name] => def, [city] => zxy, 
        answer => Array( [0] => Array ( [question_id] => 1 , [answer] => vvv)
                         [1] => Array ( [question_id] => 2 , [answer] => bbb)
                        ))
                [2] => Array ( [id] => 3 , [name] => ghi, [city] => xyz , 
        answer => Array( [0] => Array ( [question_id] => 1 , [answer] => rrr)
                         [1] => Array ( [question_id] => 2 , [answer] => sss)
                         [2] => Array ( [question_id] => 3 , [answer] => ggg)
                        )) )

Can any one help me to achieve this result with minimum sql query. I have one option to first find all user then using another mysql query inside foreach to find the answer given by user corresponding to its user_id and push into the previous array result. But with this case a lot of query were executing for large number of users. So can any-one suggest any better solution.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 微信会员卡接入微信支付商户号收款
    • ¥15 如何获取烟草零售终端数据
    • ¥15 数学建模招标中位数问题
    • ¥15 phython路径名过长报错 不知道什么问题
    • ¥15 深度学习中模型转换该怎么实现
    • ¥15 HLs设计手写数字识别程序编译通不过
    • ¥15 Stata外部命令安装问题求帮助!
    • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
    • ¥15 TYPCE母转母,插入认方向
    • ¥15 如何用python向钉钉机器人发送可以放大的图片?