MySql查询连接返回结果为空列

时间:2013-04-30 15:46:31

标签: mysql join

你好,我有这个查询,我希望它也从表j(作业卡)返回行,其中j.articleId,j.statusId,j.hcwsId,j.gpId为空任何帮助PLZ?

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"

2 个答案:

答案 0 :(得分:2)

您没有获得这些列的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

答案 1 :(得分:0)

使用LEFT JOIN。您现在拥有的查询有效地用作INNER JOIN,排除了没有匹配项的行。使用LEFT JOIN,您将返回jobcard中的所有行,如果其他表中没有匹配的记录(即当该列包含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