【问题标题】:Using ORDER BY on an INNER JOIN SQL query对 INNER JOIN SQL 查询使用 ORDER BY
【发布时间】:2023-04-02 22:53:01
【问题描述】:

我有以下三个表:Product、User 和 Purchased。

产品包含:

  • 唯一标识符 (productID)
  • 名称(产品名称)
  • 价格 (产品价格)

用户包含:

  • 唯一标识符 (userID)

购买包含:

  • 两个标识符记为私钥,productID 和 userID
  • 记录的日期(创建日期)

我的查询应返回自 1 月 1 日以来在零售商网站上购买的独特产品列表,其中最昂贵的产品首先返回。

我的查询:

SELECT Product.productID, Product.productname, Purchased.creationdate
FROM Product
INNER JOIN Purchased 
ON Product.productID = Purchased.productID;
ORDER BY Product.productprice DESC;

【问题讨论】:

  • 你有什么问题?

标签: mysql inner-join


【解决方案1】:

如果您只想要产品列表,我建议您使用exists 而不是join

select p.*
from products p
where exists (select 1
              from purchased pu
              where pu.productId = p.productId and
                    year(pu.creationdate) = year(now())
             )
order by price desc;

【讨论】:

    猜你喜欢
    • 2013-02-09
    • 1970-01-01
    • 2017-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-02
    相关资源
    最近更新 更多