1

我有以下表格:

购物中心:

+-----------+----------------------+------+-----+---------+----------------+
| Field     | Type                 | Null | Key | Default | Extra          |
+-----------+----------------------+------+-----+---------+----------------+
| MallID    | smallint(5) unsigned | NO   | PRI | NULL    | auto_increment |
| Name      | varchar(45)          | NO   |     | NULL    |                |
+-----------+----------------------+------+-----+---------+----------------+

店铺:

+------------+----------------------+------+-----+---------+----------------+
| Field      | Type                 | Null | Key | Default | Extra          |
+------------+----------------------+------+-----+---------+----------------+
| StoreID    | smallint(5) unsigned | NO   | PRI | NULL    | auto_increment |
| MallID     | smallint(5) unsigned | NO   | MUL | NULL    |                |
| Name       | varchar(45)          | NO   |     | NULL    |                |
| Revenue    | int(10)              | NO   |     | NULL    |                |
+------------+----------------------+------+-----+---------+----------------+

顾客:

+------------+----------------------+------+-----+---------+----------------+
| Field      | Type                 | Null | Key | Default | Extra          |
+------------+----------------------+------+-----+---------+----------------+
| CustomerID | smallint(5) unsigned | NO   | PRI | NULL    | auto_increment |
| StoreID    | smallint(5) unsigned | NO   | MUL | NULL    |                |
| Name       | varchar(45)          | NO   |     | NULL    |                |
| Age        | smallint(3)          | NO   |     | NULL    |                |
+------------+----------------------+------+-----+---------+----------------+

Store.MallID是的外键Mall.MallID并且Customer.StoreID是的外键Store.StoreID

1 - 我想选择所有商店总收入大于 100000 的 Mall。

select * from Mall where 100000 < ( select sum(Revenue) from Store where Mall.MallID = Store.MallID);

2 - 我想选择没有任何顾客的商场名称。

select Name from Mall where 0 = ( select count(*) from Customer, Store where Mall.MallID = Store.MallID and Store.StoreID = Customer.StoreID);

这些查询是否正确?

4

4 回答 4

2

您的查询符合您的预期。我将使用 ANSI 标准语法编写第二个join,并鼓励您使用缩写作为表别名:

select Name
from Mall m
where 0 = (select count(*)
           from Customer c join
                Store s 
                on Mall.MallID = Store.MallID 
           where Store.StoreID = Customer.StoreID
          );

另一种表述是将其全部作为单个连接和聚合来完成:

select m.Name
from Mall m left outer join
     Store s
     on s.MallID = m.MallID left outer join
     Customer c
     on s.StoredId = c.StoreId
group by m.Name
having count(c.CustomerId) = 0;

在 MySQL 中,我不鼓励您在子查询中进行聚合然后将其加入。虽然是一个非常好的 SQL 解决方案,但 MySQL 实际上会为此类子查询创建派生表,这有时会对性能产生负面影响。

于 2013-09-18T11:30:56.993 回答
2

它们是正确的,但有更好的(恕我直言)方法来编写它们:

SELECT m.*
FROM Mall m
JOIN (SELECT MallID, SUM(Revenue) totalRev
      FROM Store
      GROUP BY MallID
      HAVING totalRev > 100000) s
ON s.MallID = m.MallID

SELECT m.Name
FROM Mall m
LEFT JOIN (SELECT DISTINCT s.MallID
           FROM Customer c
           JOIN Store s
           ON c.StoreID = s.StoreID) s
ON m.MallID = s.MallID
WHERE s.MallID IS NULL
于 2013-09-18T11:18:26.997 回答
2

他们是正确的,我已经在这里为您检查过:

http://sqlfiddle.com/#!2/6a496/12

您可以使用一些建议的 sql 命令,也可以保留您的(编写查询的方法太多了!)

1.

SELECT * 
FROM Mall 
where MallID in (
      SELECT MallID
      FROM Store
      GROUP BY MallID
      HAVING SUM(Revenue) > 100000)

2.

SELECT Name
FROM Mall
WHERE (SELECT count(*) 
       FROM Customer, Store 
       WHERE Mall.MallID = Store.MallID and Store.StoreID = Customer.StoreID
      )=0;
于 2013-09-18T11:33:27.360 回答
0

我建议在查询 2 中使用外部联接,因为可能有一个没有商店的空商场,因此没有客户。

select Name from Mall where 0 = ( select count(*) from Customer, Store where Mall.MallID(+) = Store.MallID and Store.StoreID = Customer.StoreID);

谢谢尼拉吉·拉蒂

于 2013-09-18T11:17:09.870 回答