left join
stackoverflow: what is the difference between inner join and outer join
有时候左连接并不一定满足需求:
如下例子通过积分签到记录左连接用户user表,希望查询出用户的姓名,但是196的用户不存在user表中,此时leftJoin左连接就查询出来了空,用户名称都没有,这条积分记录查询出来又有什么意义呢?
SelectSeekStep1<Record, Timestamp> select = db().select(myFields)
.from(USER_SCORE.leftJoin(USER).on(USER_SCORE.USER_ID.eq(USER.USER_ID)).leftJoin(USER_TAG).on(USER_SCORE.USER_ID.eq(USER_TAG.USER_ID)))
.where(USER_SCORE.DESC.eq(VersionName.SUB_3_SIGN_SCORE).and(condition))
.groupBy(myFields)
.orderBy(USER_SCORE.CREATE_TIME.desc());
PageResult{page=Page [totalRows=2, currentPage=1, firstPage=1, prePage=1, nextPage=1, lastPage=1, pageRows=20, pageCount=1], dataList=[+-----+-------+-----+------+-------------------+------------+-----------+--------+--------+-------+----------+---------+-----------+---------------------+---------------------+----------+--------+------+
| id|user_id|score|status|flow_no |usable_score|identity_id|goods_id|order_sn|shop_id|desc |remark_id|remark_data|create_time |expire_time |admin_user|username|mobile|
+-----+-------+-----+------+-------------------+------------+-----------+--------+--------+-------+----------+---------+-----------+---------------------+---------------------+----------+--------+------+
|20024| 196| 6| 0|S202003261801505546| 6|{null} | 0| | 0|sign_score|3005 |{null} |2020-03-26 18:01:50.0|2020-03-26 18:01:50.0|0 |{null} |{null}|
+-----+-------+-----+------+-------------------+------------+-----------+--------+--------+-------+----------+---------+-----------+---------------------+---------------------+----------+--------+------+
, +-----+-------+-----+------+-------------------+------------+-----------+--------+--------+-------+----------+---------+-----------+---------------------+---------------------+----------+--------+-----------+
| id|user_id|score|status|flow_no |usable_score|identity_id|goods_id|order_sn|shop_id|desc |remark_id|remark_data|create_time |expire_time |admin_user|username|mobile |
+-----+-------+-----+------+-------------------+------------+-----------+--------+--------+-------+----------+---------+-----------+---------------------+---------------------+----------+--------+-----------+
|20023| 198| 6| 0|S202003261801505546| 6|{null} | 0| | 0|sign_score|3005 |{null} |2020-03-26 18:01:50.0|2020-03-26 18:01:50.0|0 |甜甜椒2 |17801054480|
+-----+-------+-----+------+-------------------+------------+-----------+--------+--------+-------+----------+---------+-----------+---------------------+---------------------+----------+--------+-----------+
]}
所以将left join 改为inner join,就只查询出来了我们需要的数据
SelectSeekStep1<Record, Timestamp> select = db().select(myFields)
.from(USER_SCORE.innerJoin(USER).on(USER_SCORE.USER_ID.eq(USER.USER_ID)).leftJoin(USER_TAG).on(USER_SCORE.USER_ID.eq(USER_TAG.USER_ID)))
.where(USER_SCORE.DESC.eq(VersionName.SUB_3_SIGN_SCORE).and(condition))
.groupBy(myFields)
.orderBy(USER_SCORE.CREATE_TIME.desc());
PageResult{page=Page [totalRows=1, currentPage=1, firstPage=1, prePage=1, nextPage=1, lastPage=1, pageRows=20, pageCount=1], dataList=[+-----+-------+-----+------+-------------------+------------+-----------+--------+--------+-------+----------+---------+-----------+---------------------+---------------------+----------+--------+-----------+
| id|user_id|score|status|flow_no |usable_score|identity_id|goods_id|order_sn|shop_id|desc |remark_id|remark_data|create_time |expire_time |admin_user|username|mobile |
+-----+-------+-----+------+-------------------+------------+-----------+--------+--------+-------+----------+---------+-----------+---------------------+---------------------+----------+--------+-----------+
|20023| 198| 6| 0|S202003261801505546| 6|{null} | 0| | 0|sign_score|3005 |{null} |2020-03-26 18:01:50.0|2020-03-26 18:01:50.0|0 |甜甜椒2 |17801054480|
+-----+-------+-----+------+-------------------+------------+-----------+--------+--------+-------+----------+---------+-----------+---------------------+---------------------+----------+--------+-----------+
]}