I am new at joins. I want to get sum of debts and incomes on mysql. But I am facing with a problem. The problem is sum works more than normal. Here is query
Select uyeler.*,
sum(uye_gider.tutar) as gider,
sum(gelir.tutar) as gelir
from uyeler
LEFT JOIN gelir on gelir.uye=uyeler.id
LEFT JOIN uye_gider on uye_gider.uye=uyeler.id
group by uyeler.id
First:
If I dont write group by it gives me only first row.
Does this work like this?
Main problem:
I have :
-2 row 'uye'(user)
-3 row 'gelir'(income)
-1 row 'uye_gider'(debt) value is 25
But when I execute this query the value of gider is 75.
I think its sum('uye_gider.tutar') is working 3 times because of 'gelir.tutar'
What am I doing wrong?
------Tables------
CREATE TABLE IF NOT EXISTS `gelir` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`tarih` date NOT NULL,
`uye` int(11) NOT NULL,
`tutar` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `uyeler` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`ad` varchar(15) NOT NULL,
`soyad` varchar(15) NOT NULL,
`tc` varchar(11) NOT NULL,
`dogum` date NOT NULL,
`cep` int(11) NOT NULL,
`eposta` varchar(50) NOT NULL,
`is` int(11) NOT NULL,
`daire` int(11) NOT NULL,
`kan` varchar(5) NOT NULL,
`web` varchar(12) NOT NULL,
`webpw` varchar(100) NOT NULL,
`tur` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `uye_gider` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`uye` int(11) NOT NULL,
`tutar` float NOT NULL,
`gider` int(11) NOT NULL,
`aciklama` text COLLATE utf8_bin,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=1 ;
---End Tables---