我有两个表,一个表A,一个表B
表A里有两个字段:tid(int)和authorid(int)
表B里有三个字段:tid(int)、title(char)和lasttime(int)现在我要先根据authorid=41这个条件,查出A表里所有符合条件的tid(剔除重复项):
select distinct tid from A where authorid=41
然后,再从B表里查出title字段的值,条件是tid等于从A表里查出的tid,并且按照lasttime字段进行降序排列
select title from B where B.tid=A.tid order by lasttime desc[我写的两个SQL语句只是为了把问题描述清楚]
请问这个思路应该怎么构造SQL语句呢?
表A里有两个字段:tid(int)和authorid(int)
表B里有三个字段:tid(int)、title(char)和lasttime(int)现在我要先根据authorid=41这个条件,查出A表里所有符合条件的tid(剔除重复项):
select distinct tid from A where authorid=41
然后,再从B表里查出title字段的值,条件是tid等于从A表里查出的tid,并且按照lasttime字段进行降序排列
select title from B where B.tid=A.tid order by lasttime desc[我写的两个SQL语句只是为了把问题描述清楚]
请问这个思路应该怎么构造SQL语句呢?
FROM B
INNER JOIN (SELECT DISTINCT tid FROM A WHERE A.authorid=41) tmp
ON B.tid=tmp.tid
FROM B
INNER JOIN (SELECT DISTINCT tid FROM A WHERE A.authorid=41) tmp
ON B.tid=tmp.tid
ORDER BY B.lasttime DESC
FROM B
INNER JOIN (SELECT DISTINCT tid FROM A WHERE A.authorid=41) tmp
ON B.tid=tmp.tid
ORDER BY B.lasttime DESC
from B
where tid in (select distinct tid from A where authorid=41)
order by lasttime desc
想问一下,如果要把B表里的tid也查出来的话,应该怎么写?我用你这个查别的都没问题,但是想把对应记录的tid也查出来的话就出问题了
建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
1. 你的 create table xxx .. 语句
2. 你的 insert into xxx ... 语句
3. 结果是什么样,(并给以简单的算法描述)
4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。
tid int,
authorid int
)
insert into a(tid,authorid) values('1','41')
insert into a(tid,authorid) values('2','41')
insert into a(tid,authorid) values('3','41')
insert into a(tid,authorid) values('3','41')
insert into a(tid,authorid) values('4','55')create table b(
tid int,
subject char(10),
lastpost int
)
insert into b(tid,subject,lastpost) values('1','aa','11')
insert into b(tid,subject,lastpost) values('2','bb','22')
insert into b(tid,subject,lastpost) values('3','cc','33')
insert into b(tid,subject,lastpost) values('4','dd','44')
insert into b(tid,subject,lastpost) values('5','ee','55')要 从b表里面 得到这样的结果:
tid subject lastpost
3 cc 33
2 bb 22
1 aa 11问题描述:
先从a表里面查出所有authorid=41的记录,并排除tid相同的记录
再从b表里按照从a表里查出的tid为条件,查出b.tid=a.tid这样的记录,并按照lastpost降序排列
+------+----------+
| 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>
FROM B
INNER JOIN (SELECT DISTINCT tid FROM A WHERE A.authorid=41) tmp
ON B.tid=tmp.tid
ORDER BY B.lasttime DESC