1

你好,我有这个查询,我希望它也从表 j(jobcard) 返回行,其中 j.articleId、j.statusId、j.hcwsId、j.gpId 为空,有什么帮助吗?

SELECT jobcardId,createDateTime,jobNo,companyId,
                         customerId,serialNo,rsvdDate,model,promiseDate,
                         readyDate,deliveryDate,cashMemoNo,dealer,
                         dop,status,warrantyCardno,batchNo,
                         employeeId,hcws,gp,cdId,
                         collectionDate,remarks,article 
                FROM jobcard j, articles a, statuses s, hcws h, gp g
                WHERE j.articleId=a.articleId AND
                      j.statusId = s.statusId AND
                      j.hcwsId = h.hcwsId AND
                      j.gpId=g.gpId"
4

2 回答 2

3

您没有获得NULL这些列的值,因为您的内部连接将它们排除在外 - 它们与其他表中的任何内容都不匹配。

要包含空值,请使用OUTER JOIN

SELECT jobcardId,createDateTime,jobNo,companyId,
  customerId,serialNo,rsvdDate,model,promiseDate,
  readyDate,deliveryDate,cashMemoNo,dealer,
  dop,status,warrantyCardno,batchNo,
  employeeId,hcws,gp,cdId,
  collectionDate,remarks,article 
FROM jobcard j
LEFT OUTER JOIN articles a ON j.articleId=a.articleId
LEFT OUTER JOIN statuses s ON j.statusId = s.statusId
LEFT OUTER JOIN hcws h ON j.hcwsId = h.hcwsId
LEFT OUTER JOIN gp g ON j.gpId=g.gpId
于 2013-04-30T15:56:28.163 回答
0

使用LEFT JOIN. 您现在拥有的查询有效地用作INNER JOIN,排除没有匹配项的行。使用 LEFT JOIN,您可以返回作业卡中的所有行,并返回NULL其他表中不包含匹配记录的列(即当作业卡包含NULL该列时)。

SELECT jobcardId,createDateTime,jobNo,companyId,
                         customerId,serialNo,rsvdDate,model,promiseDate,
                         readyDate,deliveryDate,cashMemoNo,dealer,
                         dop,status,warrantyCardno,batchNo,
                         employeeId,hcws,gp,cdId,
                         collectionDate,remarks,article 
FROM 
  jobcard j
  LEFT JOIN articles a on j.articleId=a.articleId
  LEFT JOIN statuses s on j.statusId = s.statusId
  LEFT JOIN hcws h j.hcwsId = h.hcwsId 
  LEFT JOIN gp g on j.gpId=g.gpId
于 2013-04-30T15:56:19.817 回答