SQL查询语句join和union

时间:2014-05-09 08:36:31

标签: vb.net

我写了一个SQL语句,但我无法得到理想的答案

Dim objDataAdapter As SqlDataAdapter = New SqlDataAdapter("SELECT FIBER.ID , LIST.STATION , LIST.CODE , FIBER.NUMBER , FIBER.OWNER , " _
+ " FIBER.KIND , FIBER.CORE_NUMBER , FIBER.LENGTH , FIBER.LOSS ,FIBER.COLOR , FIBER.CABINET , FIBER.R_NUMBER , NULL AS R_STATION , NULL AS R_CODE, FIBER.COMMENT FROM LIST " _
+ " INNER JOIN FIBER " _
 + " ON FIBER.NUMBER = LIST.NUM_CODE " _
 + " UNION  ALL" _
 + " SELECT FIBER.ID , NULL AS STATION , NULL AS CODE , FIBER.NUMBER , FIBER.OWNER , " _
+ " FIBER.KIND , FIBER.CORE_NUMBER , FIBER.LENGTH , FIBER.LOSS ,FIBER.COLOR , FIBER.CABINET , FIBER.R_NUMBER , LIST.STATION AS R_STATION , LIST.CODE AS R_CODE , FIBER.COMMENT FROM LIST " _
 + " INNER JOIN FIBER " _
+ " ON FIBER.R_NUMBER = LIST.NUM_CODE " _
, objConnection)

,答案是

![我的回答必须是三条记录,但我找回了六条记录而且R_CODE,R_STATION被复制到以下三条记录中] [10]

感谢

1 个答案:

答案 0 :(得分:1)

而不是使用" UNION ALL" ,使用" UNION"。

因为UNION删除重复记录(结果中的所有列都相同),UNION ALL不会