简体   繁体   中英

MySQL: Sub-query troubles

So I am attempting my very first sub query and ran into a small problem... Why is the query not taking into account my WHERE clause?

My query:

SELECT *
FROM Product
WHERE stdUnitPrice < 5* 
(SELECT MIN(stdUnitPrice)
FROM Product
WHERE discontinued = 'A')

But in the results I am still getting values in the discontinued column that are NOT just 'A'

Here are the results:

# productId, prodName, stdUnitPrice, qtyPerUnit, discontinued
3, Aniseed Syrup, 11.00, 12 - 550 ml bottles, A
13, Konbu, 6.60, 2 kg box, A
19, Teatime Chocolate Biscuits, 10.12, 10 boxes x 12 pieces, A
21, Sir Rodney's Scones, 11.00, 24 pkgs. x 4 pieces, A
23, Tunnbrod, 9.90, 12 - 250 g pkgs., A
**24, Guarana Fantastica, 4.95, 12 - 355 ml cans, D**
33, Geitost, 2.75, 500 g, A
41, Jack's New England Clam Chowder, 10.61, 12 - 12 oz cans, A
45, Rogede sild, 10.45, 1k pkg., A
46, Spegesild, 13.20, 4 - 450 g glasses, A
47, Zaanse koeken, 10.45, 10 - 4 oz boxes, A
52, Filo Mix, 7.70, 16 - 2 kg boxes, A
54, Tourtiere, 8.19, 16 pies, A
74, Longlife Tofu, 11.00, 5 kg pkg., A
75, Rhonbrau Klosterbier, 8.52, 24 - 0.5 l bottles, A
78, Bob's Down Home Juice, 7.90, 6 pack, A

Try this:

select *
from Product
where stdUnitPrice < (5 * (select min(stdUnitPrice) from Product ) )
 and discontinued = 'A'

It appears as though MySQL is looking at the where and thinking stdUnitPrice < 5

As well, your where needs to be on the main query.

Try moving your math into the subquery like so:

SELECT *
FROM Product
WHERE stdUnitPrice <
(SELECT 5*MIN(stdUnitPrice)
FROM Product)
    AND discontinued = 'A'

So the reasoning for this not working the way you want it to is because you are missing the where clause in your first query. Let me break it down a little for you.

select * from Product where stdUnitPrice < 5* (select min(stdUnitPrice)   from Product where discontinued = 'A')

so in this query the sub query gets executed first so lets assume that we set the sub query to a variable:

var subquery = select min(stdUnitPrice)   from Product where discontinued = 'A';

Now after that subquery is executed it is then plugged back into the original query like so: (using the new variable name)

select * from Product where stdUnitPrice < 5 * (subquery);

so to get this to include a where discontinued = 'A' you would need to change your query to the following:

SELECT * FROM Product WHERE stdUnitPrice < 5 * (SELECT MIN(stdUnitPrice) FROM Product WHERE discontinued = 'A') AND discontinued = 'A';

I hope this helps.

EDIT: Just to clarify you can't actually use a variable like that im just using that as an example to show how the query will actually be executed.

You are performing an aggregate subset operation that is filtered, however, your main query will return all rows. You are basically asking to return all rows * the minimum value in the table. You need t return the minimum value within all rows that match your selection clause.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM