简体   繁体   English

MySQL:子查询的麻烦

[英]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? 因此,我正在尝试第一个子查询并遇到一个小问题...为什么查询未考虑我的WHERE子句?

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' 但是在结果中,我仍然在非连续列中得到的值不只是“ 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 似乎MySQL正在查看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. 因此,无法按您希望的方式工作的原因是因为您在第一个查询中缺少where子句。 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: 因此,要使其包含不连续='A'的位置,您需要将查询更改为以下内容:

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. 您不需要在与选择子句匹配的所有行中返回最小值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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