本帖最后由 superman_ljz 于 2010-10-06 15:39:41 编辑

解决方案 »

  1.   

    mysql> select a.user_id,b.option_id,if(c.option_result=b.option_id,1,c.option_result) as option_result
        -> from ((select distinct user_id from choice_result) a ,choice_option b)
        ->  left join choice_result c on a.user_id=c.user_id and b.option_id=c.option_id
        -> order by 1,2;
    +---------+-----------+---------------+
    | user_id | option_id | option_result |
    +---------+-----------+---------------+
    | 1001    | 2010      | 1             |
    | 1001    | 2011      | NULL          |
    | 1001    | 2012      | NULL          |
    | 1001    | 3010      | 1             |
    | 1001    | 3011      | NULL          |
    | 1001    | 4010      | 提高服务水平  |
    | 2001    | 2010      | NULL          |
    | 2001    | 2011      | NULL          |
    | 2001    | 2012      | 朋友介绍      |
    | 2001    | 3010      | NULL          |
    | 2001    | 3011      | NULL          |
    | 2001    | 4010      | 提高产品质量  |
    +---------+-----------+---------------+
    12 rows in set (0.00 sec)mysql>
      

  2.   

    根据ACMAIN_CHM的改的,他太厉害了。。
    自己写的时候就没有思路。mysql>  select a.user_name,b.option_name,if(c.option_result=b.option_id,1,c.option_result) as option_result from
        -> ((select distinct user_id,user_name from choice_result) a,choice_option b)
        -> left join choice_result c on a.user_id=c.user_id and b.option_id=c.option_id order by 1,2;
    +-----------+-------------+---------------+
    | user_name | option_name | option_result |
    +-----------+-------------+---------------+
    | 张三        | 上网了解      | 1             |
    | 张三        | 产品质量好    | NULL          |
    | 张三        | 其他          | NULL          |
    | 张三        | 建议:        | 提高服务水平    |
    | 张三        | 性价比高      | 1             |
    | 张三        | 报纸杂志      | NULL          |
    | 李四        | 上网了解      | NULL          |
    | 李四        | 产品质量好    | NULL          |
    | 李四        | 其他          | 朋友介绍        |
    | 李四        | 建议:        | 提高产品质量    |
    | 李四        | 性价比高      | NULL          |
    | 李四        | 报纸杂志      | NULL          |
    +-----------+-------------+---------------+
    12 rows in set (0.01 sec)