MySQL Forums
Forum List  »  German

Re: COUNT() für Anzahl Produkte / rowspan
Posted by: Thomas Wiedmann
Date: April 11, 2012 03:29AM

Hallo Simon,
ich habe mal ein kleines komplettes Beispiel dazu erstellt (wie immer sehr hilfreich):

CREATE TABLE parts (
 id INT NOT NULL,
 PRIMARY KEY (id)
);

INSERT INTO parts VALUES
( 10 ),
( 11 ),
( 12 ),
( 13 ),
( 14 );


CREATE TABLE prices (
 id INT NOT NULL,
 parts_id INT NOT NULL,
 price DEC(10,2) NOT NULL,
 PRIMARY KEY (id)
);

INSERT INTO prices VALUES
( 1, 10, 100 ),
( 2, 10, 120 ),
( 3, 10, 150 ),
( 4, 11, 111 ),
( 5, 12, 112 ),
( 6, 13, 113 ),
( 7, 13, 13 ),
( 8, 14, 114 );

SELECT pr.parts_id, pr.price, (SELECT COUNT(*) FROM prices pr2
                                WHERE pr2.parts_id = pr.parts_id )
                              AS anz_preise
  FROM parts p
  JOIN prices pr
    ON pr.parts_id = p.id
ORDER BY pr.parts_id, pr.price DESC;
+----------+--------+------------+
| parts_id | price  | anz_preise |
+----------+--------+------------+
|       10 | 150.00 |          3 |
|       10 | 120.00 |          3 |
|       10 | 100.00 |          3 |
|       11 | 111.00 |          1 |
|       12 | 112.00 |          1 |
|       13 | 113.00 |          2 |
|       13 |  13.00 |          2 |
|       14 | 114.00 |          1 |
+----------+--------+------------+
8 rows in set (0.00 sec)

mysql>

Eventuell so...?

Falls dass nicht ausreicht bitte folgendes nachliefern:
* SHOW CREATE TABELE tbl; -- von allen beteiligten Tabellen
* Testdaten, die mit INSERT eingelesen werden können
* das aktuelle (falsche) Ergebnis zeigen
* das gewünschte Ergebnis zeigen

Bitte SQL Befehle einklammern mit [ code ] [ / code ] (ohne die Leerzeichen)

Grüße
Thomas

Options: ReplyQuote


Subject
Views
Written By
Posted
Re: COUNT() für Anzahl Produkte / rowspan
1520
April 11, 2012 03:29AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.