简体   繁体   English

子查询:使用 having 子句过滤掉包含子查询的 null 值的外部查询行是一种好习惯吗

[英]subquery: is it good practise to use having clause to filter out rows of outer query that contains subquery's null values

I have the following tables:我有以下表格:

SELECT * FROM medicine

| id | name        | producer |
+----+-------------+----------+
|  1 | aspirin     | pharma   |
|  2 | cetamol     | tamiko   |
|  3 | brufen      | pharma   |
|  4 | cetacodeine | adamco   |
|  5 | actifed     | tamiko   |
|  6 | claritin    | tamiko   |
+----+-------------+----------+

SELECT * FROM liquid

+-----+------+-------+
| lid | m_id | price |
+-----+------+-------+
| l1  |    2 |  1000 |
| l2  |    3 |  1500 |
| l3  |    4 |  3000 |
| l4  |    5 |  2000 |
| l5  |    6 |  1800 |
+-----+------+-------+

SELECT * FROM tablet

+-----+------+-------+
| tid | m_id | price |
+-----+------+-------+
| t1  |    1 |   900 |
| t2  |    2 |   600 |
| t3  |    3 |  1200 |
| t4  |    5 |  2000 |
+-----+------+-------+

Where m_id in the last two tablest has foriegn key referencing to id in the first table.其中最后两个表中的m_id具有引用第一个表中的id的外键。

I'm trying to list lid and name of medicines that are liquid and produced only by producer tamiko Executing the following statement will result in rows that contains null values returned from the subquery and I want to omit我正在尝试列出仅由生产商tamiko生产的液体药物的盖子名称执行以下语句将导致包含从子查询返回的 null 值的行,我想省略

SELECT lid, (SELECT name FROM medicine WHERE liquid.m_id=medicine.id AND medicine.producer='tamiko') AS byTamiko FROM liquid;

+-----+----------+
| lid | byTamiko |
+-----+----------+
| l1  | cetamol  |                                                                                 
| l2  | NULL     |                                                                                 
| l3  | NULL     |                                                                                  
| l4  | actifed  |                                                                                 
| l5  | claritin |                                                                                
+-----+----------+

So I had some tries and using HAVING clause seems like a solution所以我做了一些尝试,使用HAVING子句似乎是一个解决方案

SELECT lid, (SELECT name FROM medicine WHERE liquid.m_id=medicine.id AND medicine.producer='tamiko') AS byTamiko FROM liquid HAVING byTamiko IS NOT NULL;

| lid | byTamiko |
+-----+----------+
| l1  | cetamol  |
| l4  | actifed  |
| l5  | claritin |
+-----+----------+

However, my teacher disagreed saying that HAVING clause should be only used with aggregated functions and along with GROUP BY statement.但是,我的老师不同意HAVING子句只能与聚合函数和GROUP BY语句一起使用。 I'm now confused whether this is an appropriate usage or not?我现在很困惑这是否是适当的用法? and what happened exactly that made it work?究竟发生了什么使它起作用?

Note: I have to do this query using sub queries only and not any JOIN or different statement.注意:我必须只使用子查询而不是任何JOIN或不同的语句来执行此查询。

Note2: Be aware that This is not a duplicated, I'm trying to get an explanation on how it worked and whether it's an correct usage or not.注意2:请注意,这不是重复的,我试图解释它是如何工作的以及它是否是正确的用法。

It is not really appropriate.这不太合适。

In the SQL Standard having is defined as filtering after aggregation and where is for filtering before aggregation (if there is any aggregation).在 SQL 标准having有定义为聚合的过滤,而where是聚合前的过滤(如果有任何聚合)。

MySQL extends this to allow having in non-aggregation queries. MySQL 对此进行了扩展,以允许having非聚合查询。 This extension allows you to filter on column aliases without using a subquery.此扩展允许您在不使用子查询的情况下过滤列别名。 But you should use where for filtering before aggregation.但是您应该在聚合之前使用where进行过滤。

so it should look like this?所以它应该看起来像这样?

SELECT lid, (
    SELECT name 
    FROM medicine 
    WHERE liquid.m_id=medicine.id AND medicine.producer='tamiko'
) AS byTamiko 
FROM liquid 
WHERE byTamiko IS NOT NULL;

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

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