mysql 连表查询 本帖最后由 superman_ljz 于 2010-10-06 15:39:41 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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> 根据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) 一张表中两个字段需同时要默认获取当时时间,如何创建触发器 mysqldump的一个错误 带回复的留言板构造MYSQL数据库问题 MySql判断图片是否存在? 求一sql语句 如何解决jsp和my sql数据库的连接驱动问题? MySQL数据导入/导出问题 不调用mysql_store_result() 只用执行了mysql_query()需要mysq_free_result()吗 mysql select不可以赋值给变量么? 请教,mysql cluster监控 sql 字段记录里有中文和英文,数字数据我怎么查非英文的数据 数据复制到临时表的问题
-> 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>
自己写的时候就没有思路。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)