日期:2014-05-16  浏览次数:20701 次

请教了两天的问题,老师也不会。MYSQL
表php188_member
值parent_cp_member_active,mem_id,mem_username

要求:如果parent_cp_member_active等于mem_id.则输出mem_username

用了IN、join、自连接。。。
要么空表,要么错误,纠结的头都大了。
那个好心的哥哥姐姐能赐教下。


------解决方案--------------------
直接用select子句就行了:
1. 如果直接能找到就不用ifnull判断:
如:
SQL code
mysql> create table hj89 (mem_id integer primary key, mem_username varchar(32), parent_cp_member_active varchar(32));
Query OK, 0 rows affected (0.17 sec)

mysql> insert into hj89 values(32687, '张三', 32457);
Query OK, 1 row affected (0.06 sec)

mysql> insert into hj89 values(32457, '李四', 32687);
Query OK, 1 row affected (0.05 sec)

mysql> select mem_id, mem_username, (select mem_username from hj89 where mem_id = c.parent_cp_member_active) from hj89 c;
+--------+--------------+--------------------------------------+
| mem_id | mem_username | (select mem_username from hj89 where mem_id = c.parent_cp_member_active) |
+--------+--------------+--------------------------------------+
|  32457 | 李四         | 张三                                                                     |
|  32687 | 张三         | 李四                                                                     |
+--------+--------------+--------------------------------------+
2 rows in set (0.03 sec)