日期:2014-05-16 浏览次数:20833 次
SELECT title FROM B INNER JOIN (SELECT DISTINCT tid FROM A WHERE A.authorid=41) tmp ON B.tid=tmp.tid ORDER BY B.lasttime DESC
------解决方案--------------------
mysql> select * from a;
+------+----------+
| tid | authorid |
+------+----------+
| 1 | 41 |
| 2 | 41 |
| 3 | 41 |
| 3 | 41 |
| 4 | 55 |
+------+----------+
5 rows in set (0.00 sec)
mysql> select * from b;
+------+---------+----------+
| tid | subject | lastpost |
+------+---------+----------+
| 1 | aa | 11 |
| 2 | bb | 22 |
| 3 | cc | 33 |
| 4 | dd | 44 |
| 5 | ee | 55 |
+------+---------+----------+
5 rows in set (0.00 sec)
mysql> select * from b
-> where exists (select 1 from a where tid=b.tid and authorid=41)
-> order by lastpost desc
-> ;
+------+---------+----------+
| tid | subject | lastpost |
+------+---------+----------+
| 3 | cc | 33 |
| 2 | bb | 22 |
| 1 | aa | 11 |
+------+---------+----------+
3 rows in set (0.00 sec)
mysql>
------解决方案--------------------
或者
mysql> select * from b
-> where tid in (select tid from a where authorid=41)
-> order by lastpost desc;
+------+---------+----------+
| tid | subject | lastpost |
+------+---------+----------+
| 3 | cc | 33 |
| 2 | bb | 22 |
| 1 | aa | 11 |
+------+---------+----------+
3 rows in set (0.00 sec)
mysql>