MySQL语句中 LEFT JOIN 、INNER JOIN 、RIGHT JOIN 区别

今天遇见一个吭,许是我没有理解清楚三者之间的联系。

不多说,直接上代码:

MySQL查询语句:

SELECT u.* FROM tb_active_user AS u LEFT JOIN tb_active_team AS t ON t.user_id = #{userId} AND t.active_user_id = u.active_user_id AND t.delete_flag = 0 AND u.delete_flag = 0 AND u.active_id = #{activeId}

结果:

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned by selectOne(), but found: 4
	at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:77)
	at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446)
	at com.sun.proxy.$Proxy71.selectOne(Unknown Source)
	at org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:166)
	at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:83)
	at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59)
	at com.sun.proxy.$Proxy705.getActiveDetails(Unknown Source)
	at com.bicycle.weixin.service.impl.WeiXinUserServiceImpl.getActiveDetails(WeiXinUserServiceImpl.java:265)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:343)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:197)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
	at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294)
	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
	at com.sun.proxy.$Proxy726.getActiveDetails(Unknown Source)
	at com.bicycle.weixin.controller.UserController.getActiveDetails(UserController.java:523)
	at sun.reflect.NativeMethodA

结果报错了,意思是查询出四条数据,但是接受的只有一条。

然后想了想关于 LEFT JOIN 、INNER JOIN 、RIGHT JOIN 三者之间的区别:

INNER JOIN  是内连接,两张表之间查询指定字段

LEFT  JOIN  是左外链接,查询左边表的全部数据,也是我刚刚报错的原因

RIGHT  JOIN  是右外链接,查询右边表的全部数据

吃一线,长一智。要不断的学习,才能减少代码错误的书写。

猜你喜欢

转载自blog.csdn.net/ma919755374/article/details/82858621