Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Odejmowanie sum kolumn z dwóch różnych tabel
drager
post
Post #1





Grupa: Zarejestrowani
Postów: 7
Pomógł: 0
Dołączył: 16.03.2010

Ostrzeżenie: (0%)
-----


Witam, mam taki problem
2 tabele o różnej liczbie rekordów; 1 tabela- wydano, 3 kolumny id(int), nr(varchar), materiał(int) i 2 tabela zrobiono 3 kolumny id(int), nr(varchar), części(int).
Relacja między tymi dwoma kolumnami to wydano.nr=zrobiono.nr. Jak odjąć sumę kolumny części od sumy kolumny materiał aby wartość zawracana
była poprawna arytmetycznie.
Kod:
SELECT SUM(wydano.material) - SUM(zrobiono.czesci) from material LEFT JOIN zrobiono ON material.nr=zrobiono.nr;
podaje wynik ale błędny.
Poproszę o pomoc.
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
drager
post
Post #2





Grupa: Zarejestrowani
Postów: 7
Pomógł: 0
Dołączył: 16.03.2010

Ostrzeżenie: (0%)
-----


  1. CREATE TABLE `zrobiono` (`id` INT NOT NULL AUTO_INCREMENT ,`nr_grupy` VARCHAR( 20 ) NOT NULL ,`zrobione` INT( 4 ) NOT NULL , UNIQUE (
  2. `id`));


  1. CREATE TABLE `wydano` (`id` INT NOT NULL AUTO_INCREMENT ,`nr_grupy` VARCHAR( 20 ) NOT NULL ,`material` INT( 4 ) NOT NULL ,UNIQUE (
  2. `id`
  3. )


  1. INSERT INTO `zrobiono` ( `id` , `nr_grupy` , `zrobione` ) VALUES ('', 'a', '80');
  2. INSERT INTO `zrobiono` ( `id` , `nr_grupy` , `zrobione` ) VALUES ('', 'b', '60');
  3. INSERT INTO `zrobiono` ( `id` , `nr_grupy` , `zrobione` ) VALUES ('', 'c', '50');


  1. INSERT INTO `wydano` ( `id` , `nr_grupy` , `material` ) VALUES ('', 'a', '100');
  2. INSERT INTO `wydano` ( `id` , `nr_grupy` , `material` ) VALUES ('', 'b', '100');
  3. INSERT INTO `wydano` ( `id` , `nr_grupy` , `material` ) VALUES ('', 'c', '100');
  4. INSERT INTO `wydano` ( `id` , `nr_grupy` , `material` ) VALUES ('', 'a', '50');
  5. INSERT INTO `wydano` ( `id` , `nr_grupy` , `material` ) VALUES ('', 'c', '80');


Wydano 430, zrobiono 190 czyli sum(wydano.material) - sum(zrobiono.zrobione) powinno być 240.
  1. SELECT SUM(wydano.material) - SUM(zrobiono.zrobione) FROM wydano LEFT JOIKN zrobiono ON wydano.nr_grupy=zrobiono.nr_grupy

Źle liczy.
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 15.10.2025 - 04:23