表 a id
1
2
表 baid content
1 asss
1 sf
2 示范法
2 不被
联查后得到
id content
1 asss
null sf
2 示范法
null 不被
就是id相同的给值空
1
2
表 baid content
1 asss
1 sf
2 示范法
2 不被
联查后得到
id content
1 asss
null sf
2 示范法
null 不被
就是id相同的给值空
+------+
| 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>
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