gpt4 book ai didi

mysql - 在 INNER JOIN SQL 查询上使用 ORDER BY

转载 作者:行者123 更新时间:2023-11-29 18:31:32 24 4
gpt4 key购买 nike

我有以下三个表:产品、用户和已购买。

产品包含:

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

用户包含:

  • 唯一标识符(用户 ID)

购买的内容包括:

  • 两个标识为私钥的标识符: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;

最佳答案

如果您只想要产品列表,我建议使用 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;

关于mysql - 在 INNER JOIN SQL 查询上使用 ORDER BY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45650322/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com