与WHERE和FROM相同的SQL子查询

时间:2016-07-23 10:21:59

标签: mysql sql

使用下表

只要有需要,它也可以很容易地用OUTER JOIN替换。

WHERE语法是面向关系模型的更多。

两个表的结果JOIN'ed是应用过滤器的表的笛卡尔积,它只选择那些具有匹配连接列的行。

使用WHERE语法更容易看到这一点。

SELECT Toy_name, Quantity
FROM Toy T,Hire Transaction H, Store S
WHERE S.Store_ID = T.store_id 
AND t.toy_id = H.toy_id
AND t.hire_price = (SELECT max(hire_price) from Toy)
AND UPPER(S.store_suburb = ‘SCARSDALE’)
AND H.hire_date >= ’01/02/2013’
AND H.hire_date <= ’31/03/2015’;

我能写出来。但是如何使用子查询来解决这个问题呢?

3 个答案:

答案 0 :(得分:1)

您可以通过按降序排序结果,然后将输出限制为仅一条记录来避免子选择:

This file name

答案 1 :(得分:0)

你可以试试这个

SELECT t.toy_name, sum(ht.quantity) quantity, max(t.hire_price) max_p
  FROM toy t
  INNER  JOIN hire_transaction ht on t.toy_id = ht.toy_id
  INNER  JOIN store s on t.store_id = s.store_id
WHERE ht.hire_date between '2013-02-01' and '2015-03-31' and upper(s.store_suburb) = 'SCARSDALE'
group by t.toy_id
having max(max_p)

答案 2 :(得分:0)

从HireTransaction中选择h.toy_id,sum(h.Quantity)作为hire_count,其中h.hire_date&gt; = '01 / 02/2013' 和h.hire_date&lt; = '31 / 03/2015'由h.toy_id组成h.toy_id =(从玩具中选择toy_id = hire_price =(从玩具中选择max(hire_price))和store_id =(从商店选择store_id) store_suburb ='SCARSDALE')