【发布时间】:2020-08-03 17:51:25
【问题描述】:
问题是: 查找价格最高的打印机型号。结果集:型号、价格
数据库架构是: 打印机(代码、型号、颜色、类型、价格)
我想使用自连接找到最高价格。我写的查询是:
select model,price
from printer
where not exists
(
select p2.model,p2.price
from printer p1, printer p2
where p2.price<p1.price
)
子查询似乎是正确的,因为它不包括最高的,但整个查询给出的是空表。 问题的链接是:http://www.sql-ex.ru/learn_exercises.php#answer_ref
【问题讨论】:
-
您想查看多少种打印机型号?如果只有一个,为什么不
SELECT model, price FROM printer ORDER BY price DESC LIMIT 1? -
请在代码问题中给出minimal reproducible example--cut & paste & runnable code,包括最小的代表性示例输入为代码;期望和实际输出(包括逐字错误消息);标签和版本;明确的规范和解释。给出您可以给出的最少代码,即您显示的代码可以通过您显示的代码扩展为不正常。 (调试基础。)对于包含 DBMS 和 DDL(包括约束和索引)和输入为格式化为表的代码的 SQL。 How to Ask 停止尝试编写您的总体目标并从给定的代码中解释您的期望以及原因。
标签: mysql sql join outer-join self-join