我有三个表:客户,收据,薪水
Customers
Id Name
1 john
Receipts
Id Amount
1 500
2 250
3 600
4 700
Salary
Id Amount
1 300
2 300
3 680
我想找一些与约翰相关的工资和收据。 我认为这是简单的sql语句,如:
select cus.Name, sum(sal.Amount) as Salary, sum(re.Amount) as Recieved
from Customers as cus inner join Salaries as sal on cus.Id = sal.Id
inner join Receipts as re on cus.Id = re.Id
where cus.Id= 1
Group by cus.Name
但我发现结果更大
Name Salary Recieved
john 6150 4320
当我编写没有sum的查询时:
select cus.Name, sal.Amount as Salary, re.Amount as Recieved
from Customers as cus inner join Salaries as sal on cus.Id = sal.Id
inner join Receipts as re on cus.Id = re.Id
where cus.Id= 1
我得到了这些重复记录
john 500 100
john 500 300
john 500 680
john 250 100
john 250 300
john 250 680
john 600 100
john 600 300
john 600 680
john 700 100
john 700 300
john 700 680
为什么内部联接行为像这样?我希望得到一次记录,但重复4次! 我是否必须尝试子选择查询?
答案 0 :(得分:0)
查询看起来正确。
http://www.sqlfiddle.com/#!9/1b5157/1
您的近似查询有一个小提琴,它按预期工作。因此,如前所述,由于您有CustomerOrSupplier,您可能会加入错误的表
答案 1 :(得分:0)
select cus.Name,
sum(sal.Amount) as Salary,
sum(re.Amount) as Recieved
from Customers as cus
inner join Salary as sal
on cus.Id = sal.Id
inner join
Receipts as re
on cus.Id = re.Id
Group by cus.Name;
<强>演示强>
答案 2 :(得分:0)
我发现问题恰好是表名称之一。
更改以下内容:
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyVetoException;
import java.beans.VetoableChangeListener;
public class Square extends Canvas implements VetoableChangeListener, PropertyChangeListener {
private final int SIZE = 100;
private int side = 1;
public Square() {
setSize(SIZE,SIZE);
}
public void vetoableChange(PropertyChangeEvent pce) throws PropertyVetoException {
if ((pce.getPropertyName()).equals("value")) {
int v = (Integer)pce.getNewValue();
if ((v <=0)||(v > SIZE/2))
throw new PropertyVetoException ("Value out of bounds!", pce);
}
}
public void propertyChange(PropertyChangeEvent pce) {
if ((pce.getPropertyName()).equals("value")) {
setSide((Integer)pce.getNewValue());
repaint();
}
}
public void setSide(int side) {
this.side = side;
}
public int getSide() {
return this.side;
}
public void paint (Graphics g) {
Dimension d = getSize();
g.setColor(Color.BLUE);
g.drawRect(d.width/2 - side, d.height/2 - side, side*2, side*2);
}
}
为:
from Customers as cus inner
join Salaries as sal
on cus.Id = sal.Id