如何连接多个表而没有重复的结果?

时间:2018-04-06 10:53:13

标签: mysql sql mariadb

Hello所有StackOverFlow系列。 我需要你的帮助有关mysql中的SQL查询。我加入了四个表但结果是重复行。

我尝试使用GROUP BY但没有工作。

这是我的问题:

12:36:43.044 [clientInboundChannel-7] ERROR org.springframework.web.socket.messaging.WebSocketAnnotationMethodMessageHandler - Unhandled exception
org.springframework.messaging.MessageHandlingException: Missing path template variable 'id' for method parameter type [class java.lang.Long]
    at org.springframework.messaging.handler.annotation.support.DestinationVariableMethodArgumentResolver.handleMissingValue(DestinationVariableMethodArgumentResolver.java:70) ~[spring-messaging-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.messaging.handler.annotation.support.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:96) ~[spring-messaging-4.2.4.RELEASE.jar:4.2.4.RELEASE]

(...)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [?:1.8.0_144]
        at java.lang.Thread.run(Thread.java:748) [?:1.8.0_144]

你可以看作图片:https://imgur.com/gallery/1Ewku

这是关系表:https://imgur.com/gallery/ziKq3

我想要的结果如下:https://imgur.com/gallery/6NJpR

感谢您为这个问题付出宝贵的时间。

2 个答案:

答案 0 :(得分:1)

请尝试

  SELECT distinct `tbl_leave`.`id`, `tbl_leave`.`staff_id`,    
 `tbl_leave`.`type_id`, `tbl_leave`.`start_date`, `tbl_leave`.`end_date`, 
 `tbl_leave`.`total_days`, `tbl_leave`.`reason`, `tbl_leave_type`.`type`,
`tbl_employment`.`com_id` as `comid`, `tbl_staff`.`name`
 FROM `tbl_leave` JOIN 
 `tbl_leave_type`
 ON `tbl_leave_type`.`id` = `tbl_leave`.`type_id` JOIN
 `tbl_employment`
 ON `tbl_employment`.`staff_id` = `tbl_leave`.`staff_id` JOIN     
 `tbl_staff`
 ON `tbl_staff`.`id` = `tbl_leave`.`staff_id`;

答案 1 :(得分:1)

请尝试此解决方案并告诉我是否有任何问题:

SELECT [Columns] From tbl_staff as staff
JOIN tbl_employment as emp on staff.id = = emp.staff_id 
JOIN tbl_leave as leave on staff.id = = leave.staff_id 
JOIN tbl_leave_type as ltype on leave.type_id = = ltype.id