在查询偏斜结果集中添加where子句

时间:2019-08-10 19:58:06

标签: mysql sql

我正在尝试包括日历表中的所有数据,而仅包括销售表中符合where条件的数据。我的问题是,我正在执行的查询仅返回WHERE人员的销售月份。

应如何更改此MySQL查询,以便返回LEFT表中的所有内容?

 Create Table InvoiceDataCalendar (
  id INT,
  cal_month_year varchar(90)
  );

 Create Table Sales (
     salesperson varchar(50),
     sale_month_year varchar(90),
     sale_amount int
   );

 INSERT INTO Sales Values ('Bruce 1', 'Jan 18', 200);
 INSERT INTO Sales Values ('Bruce 1', 'Feb 19', 400);
  INSERT INTO Sales Values ('Jimmy 2', 'Jan 18', 200);
 INSERT INTO Sales Values ('Jimmy 2', 'Feb 19', 400);
 Insert Into InvoiceDataCalendar VALUES (1, 'Jan 18');
 Insert Into InvoiceDataCalendar VALUES  (2, 'Jan 19');
 Insert Into InvoiceDataCalendar VALUES  (3, 'Feb 18');
  Insert Into InvoiceDataCalendar VALUES  (4, 'Feb 19');
 Insert Into InvoiceDataCalendar VALUES  (5, 'Mar 18');
 Insert Into InvoiceDataCalendar  VALUES (6, 'Mar 19');
  Insert Into InvoiceDataCalendar VALUES  (7, 'Apr 18');
 Insert Into InvoiceDataCalendar VALUES  (8, 'Apr 19');
 Insert Into InvoiceDataCalendar VALUES  (9, 'May 18');
  Insert Into InvoiceDataCalendar VALUES  (10, 'May 19');
 Insert Into InvoiceDataCalendar VALUES  (11,'Jun 18');
 Insert Into InvoiceDataCalendar  VALUES (12, 'Jun 19');
  Insert Into InvoiceDataCalendar VALUES  (13, 'Jul 18');
 Insert Into InvoiceDataCalendar VALUES  (14, 'Jul 19');
 Insert Into InvoiceDataCalendar  VALUES (15,'Aug 18');
  Insert Into InvoiceDataCalendar VALUES (16, 'Aug 19');
 Insert Into InvoiceDataCalendar VALUES (17, 'Sep 18');
 Insert Into InvoiceDataCalendar VALUES (18, 'Sep 19');
  Insert Into InvoiceDataCalendar VALUES (19, 'Oct 18');
 Insert Into InvoiceDataCalendar VALUES (20, 'Oct 19');
 Insert Into InvoiceDataCalendar VALUES (21, 'Nov 18');
  Insert Into InvoiceDataCalendar VALUES (22, 'Nov 19');
 Insert Into InvoiceDataCalendar VALUES (23, 'Dec 18');
 Insert Into InvoiceDataCalendar VALUES (24, 'Dec 19');


 Select 
ic.cal_month_year
,s.salesperson
,SUM(s.sale_amount) As TotalSales
FROM InvoiceDataCalendar ic
LEFT JOIN Sales s
ON ic.cal_month_year = s.sale_month_year
WHERE s.salesperson = 'Bruce 1'
GROUP BY ic.cal_month_year, s.salesperson, ic.id
ORDER BY id ASC;

3 个答案:

答案 0 :(得分:2)

您不应该在where条件下使用左联接表的列,因为这种方式可以用作内部联接
而是将条件添加到ON子句

 public GameObject explosion;
 private void OnTriggerEnter2D(Collider2D enter)
 {
 if (enter.gameObject.tag.Equals("Player"))
 {
     HartCount.HartValue -= 1;
     Instantiate (explosion, transform.position, Quaterion.identity);
     Destroy(this.gameObject);
 }
 }

答案 1 :(得分:0)

只需将WHERE替换为AND即可获得所有结果:

SELECT
ic.cal_month_year
,s.salesperson
,SUM(s.sale_amount) As TotalSales
FROM InvoiceDataCalendar ic
LEFT JOIN Sales s
ON ic.cal_month_year = s.sale_month_year
AND s.salesperson = 'Bruce 1'
GROUP BY ic.cal_month_year, s.salesperson, ic.id
ORDER BY id ASC;

因为您的LEFT JOIN方法也是检索不匹配月份的正确方法。

答案 2 :(得分:0)

执行此操作时,将右表转换为子查询并将WHERE子句放在子查询中对我来说更有意义。我认为这是“预加入WHERE子句”。

Select 
ic.cal_month_year
,s.salesperson
,SUM(s.sale_amount) As TotalSales
FROM InvoiceDataCalendar ic
LEFT JOIN (select * from Sales where salesperson = 'Bruce 1') s
ON ic.cal_month_year = s.sale_month_year
GROUP BY ic.cal_month_year, s.salesperson, ic.id
ORDER BY id ASC;