这样的两表联查能否实现

renzaijiang 2010-01-06 02:16:17
表 a

id
1
2
表 b

aid content
1 asss
1 sf
2 示范法
2 不被
联查后得到
id content
1 asss
null sf
2 示范法
null 不被
就是id相同的给值空
...全文
57 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
wwwwb 2010-01-06
  • 打赏
  • 举报
回复
加入自增字段ID
SELECT iif(ma is not null,null,a.aid),a.content from tt4 a left join (select aid,max(id) as ma from tt4 group by aid) b
on a.aid=b.aid and a.id=b.ma
ACMAIN_CHM 2010-01-06
  • 打赏
  • 举报
回复
[Quote]content <t.content 何解啊 [/Quote]排序。
renzaijiang 2010-01-06
  • 打赏
  • 举报
回复
content<t.content 何解啊 大师
ACMAIN_CHM 2010-01-06
  • 打赏
  • 举报
回复
可以用SQL语句实现,但极不建议如此做法。这种功能应该是在你的程序页面处理中来实现。

mysql> select * from a;
+------+
| id |
+------+
| 1 |
| 2 |
+------+
2 rows in set (0.00 sec)

mysql> select * from b;
+------+---------+
| aid | content |
+------+---------+
| 1 | asss |
| 1 | sf |
| 2 | 示范法 |
| 2 | 不被 |
+------+---------+
4 rows in set (0.00 sec)

mysql> select
-> case when
-> (select count(*) from b where aid=t.aid and content<t.content)=0

-> then a.id end as id,
-> t.content
-> from a inner join b t on a.id=t.aid;
+------+---------+
| id | content |
+------+---------+
| 1 | asss |
| NULL | sf |
| 2 | 示范法 |
| NULL | 不被 |
+------+---------+
4 rows in set (0.00 sec)

mysql>

56,675

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧