Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> 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
Mchl
post
Post #2





Grupa: Zarejestrowani
Postów: 855
Pomógł: 145
Dołączył: 17.07.2008
Skąd: High Memory Area

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


INNER JOIN
Go to the top of the page
+Quote Post
marlic
post
Post #3





Grupa: Zarejestrowani
Postów: 12
Pomógł: 2
Dołączył: 14.03.2010

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


spróbuj tak:

  1. SELECT SUM(wydano.material) - SUM(COALESCE(zrobiono.czesci,0)) FROM material LEFT JOIN zrobiono ON material.nr=zrobiono.nr;
Go to the top of the page
+Quote Post
drager
post
Post #4





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

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


ten sam błąd arytmetyczny jak w przypadku mojego zapytania.

próbowałem INNER zawraca taki zam błąd jak w przypadku LEFT JOIN. Wykonuje zapytanie ale podaje błędny wynik
Go to the top of the page
+Quote Post
Mchl
post
Post #5





Grupa: Zarejestrowani
Postów: 855
Pomógł: 145
Dołączył: 17.07.2008
Skąd: High Memory Area

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


Mógłbyś wkleić przykładowe dane i wynik jakiego oczekujesz?
Być może musisz sobie zaemulować FULL OUTER JOIN (MySQL nie obsługuje tego natywnie więc trzeba połączyć LEFT JOIN i RIGHT JOIN)

Ten post edytował Mchl 16.03.2010, 23:21:13
Go to the top of the page
+Quote Post
drager
post
Post #6





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
Mchl
post
Post #7





Grupa: Zarejestrowani
Postów: 855
Pomógł: 145
Dołączył: 17.07.2008
Skąd: High Memory Area

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


No tak. Tutaj to złączenie nie ma żadnego sensu. Po prostu liczysz sumę w jednej tabeli, sumę w drugiej i odejmujesz

Kod
SELECT wydano.material - zrobiono.zrobione FROM
(SELECT SUM(material) AS material FROM wydano) AS wydano,
(SELECT SUM(zrobione) AS zrobione FROM zrobiono) AS zrobiono
Go to the top of the page
+Quote Post
drager
post
Post #8





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

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


Zawraca błąd

#1064 - Something is wrong in your syntax obok 'SELECT SUM(material) AS material FROM wydano) AS wydano,
(SELEC' w linii 2

Go to the top of the page
+Quote Post
piotrdd2
post
Post #9





Grupa: Zarejestrowani
Postów: 136
Pomógł: 2
Dołączył: 13.05.2003
Skąd: Przemyśl

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


  1. SELECT (SUM(wydano.material) - (SELECT sum(zrobiono.zrobione) FROM zrobiono)) AS wynik FROM wydano


wynik =240

Ten post edytował piotrdd2 17.03.2010, 13:49:17
Go to the top of the page
+Quote Post
drager
post
Post #10





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

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


SELECT (SUM(wydano.material) - (SELECT sum(zrobiono.zrobione) FROM zrobiono)) AS wynik FROM wydano

zawraca również błąd;
#1064 - Something is wrong in your syntax obok 'SELECT sum(zrobiono.zrobione) FROM zrobiono)) AS wynik FROM wyda' w linii 1
Go to the top of the page
+Quote Post
piotrdd2
post
Post #11





Grupa: Zarejestrowani
Postów: 136
Pomógł: 2
Dołączył: 13.05.2003
Skąd: Przemyśl

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


Cytat(drager @ 17.03.2010, 16:35:00 ) *
SELECT (SUM(wydano.material) - (SELECT sum(zrobiono.zrobione) FROM zrobiono)) AS wynik FROM wydano

zawraca również błąd;
#1064 - Something is wrong in your syntax obok 'SELECT sum(zrobiono.zrobione) FROM zrobiono)) AS wynik FROM wyda' w linii 1



coś sknociłeś, bo działa bez problemu.
Go to the top of the page
+Quote Post
Mchl
post
Post #12





Grupa: Zarejestrowani
Postów: 855
Pomógł: 145
Dołączył: 17.07.2008
Skąd: High Memory Area

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


Która wersja MySQL?
Go to the top of the page
+Quote Post
drager
post
Post #13





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

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


Wersja MySQL?

leciwa 3.23, może mieć wpływ?
Go to the top of the page
+Quote Post
icetique
post
Post #14





Grupa: Zarejestrowani
Postów: 72
Pomógł: 13
Dołączył: 12.04.2009

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


Porównaj PHP3 i PHP5. Widać różnicę?(IMG:style_emoticons/default/winksmiley.jpg)
Go to the top of the page
+Quote Post
Mchl
post
Post #15





Grupa: Zarejestrowani
Postów: 855
Pomógł: 145
Dołączył: 17.07.2008
Skąd: High Memory Area

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


Kolejna ofiara Krasnala?

MySQL do wersji 4.1 nie obsługiwał podzapytań (a z tego tutaj korzystamy)

Poza tym już od ponad roku wersje 4.x nie są wspierane przez producenta. 3.x to po prostu archeologia.
Go to the top of the page
+Quote Post
drager
post
Post #16





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

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


Zgadza się, wywalam to g.. i instaluję to co trzeba.

Pozdrawiam i dzięki.

SELECT (SUM(wydano.material) - (SELECT sum(zrobiono.zrobione) FROM zrobiono)) AS wynik FROM wydano


Zapytanie śmiga jak należy.
Go to the top of the page
+Quote Post

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.09.2025 - 15:23