MySQL Forums
Forum List  »  German

Re: Subquery returns more than 1 row
Posted by: Thomas Wiedmann
Date: January 06, 2012 11:35AM

Mark Knochen Wrote:
-------------------------------------------------------

> Mir ist auch klar, dass es theoretisch natürlich
> auch eine WHERE / JOIN Abfrage bringen kann, aber
> die Abfrage oben ist sehr stark gekürzt - da
> hängt noch bedeutend mehr dran.

Nach wie vor verstehe ich es nicht, warum es so schwierig ist, ein sinnvolles nachvollziehbares kleines Beispiel zu erstellen. Na ja, schaue ich mal in meine Glaskugel und mach eine kleine Lösung die "..sehr stark gekürzt.." ist und eventuell nichts mit der Frage zu tun hat.

CREATE TABLE items (
 ItemName VARCHAR(10) NOT NULL,
 itemId INT NOT NULL
);

INSERT INTO items VALUES
( 'Suit1' , 1 ),
( 'Suit2' , 2 ),
( 'Suit3' , 3 );


CREATE TABLE items_stock (
 itemStockId INT NOT NULL,
 itemId INT NOT NULL,
 PhysicalStock INT NOT NULL
);

INSERT INTO items_stock VALUES
( 1, 1, 10 ),
( 2, 1, 5 ),
( 3, 2, 20 ),
( 4, 2, 5 ),
( 5, 3, 30 ),
( 6, 3, 5 );

SELECT
  i.itemname,
  ( SELECT SUM(its.PhysicalStock)
      FROM items_stock AS its
     WHERE its.itemId = i.itemId
   ) AS stockValue
 FROM items i
WHERE i.ItemName LIKE 'Sui%';
+----------+------------+
| itemname | stockValue |
+----------+------------+
| Suit1    |         15 |
| Suit2    |         25 |
| Suit3    |         35 |
+----------+------------+
3 rows in set (0.00 sec)

mysql>

Grüße
Thomas

Options: ReplyQuote


Subject
Views
Written By
Posted
3104
December 15, 2011 08:48AM
1784
December 15, 2011 12:41PM
1604
January 06, 2012 09:51AM
Re: Subquery returns more than 1 row
1843
January 06, 2012 11:35AM
1258
January 11, 2012 03:55AM
1294
January 11, 2012 08:26AM
1496
January 12, 2012 04:52AM
2043
January 12, 2012 06:33AM
1529
January 13, 2012 03:27AM


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.