一个关于 SELECT 的 SQL 查询小问题

发布于 2024-10-24 19:45:58 字数 704 浏览 1 评论 0原文

SQL 查询仍然是我的弱点,所以这里我还有另一个 SQL 问题。

假设我有两个表:拍卖出价。表拍卖包含我的拍卖,表出价包含每次拍卖的出价列表。

现在我选择这样的值:

   SELECT
   `auction_title`,
   `auction_seo_title`,
   `auction_description_1`,
   `auction_unixtime_expiration`,
   `auction_startPrice`,
   MAX(`bids`.`bid_price`) as `bid_price`
   FROM
   `auctions`
   LEFT JOIN `bids` ON `auctions`.`auction_id`=`bids`.`bid_belongs_to_auction`
   ORDER BY
   `auction_unixtime_expiration`
   ASC
   LIMIT 5

查询有效,但有一点问题:它仅选择那些在出价中具有至少一个对应值的拍卖强>表。这意味着,如果我有一个新的拍卖,但还没有出价,查询不会返回此拍卖,但我也想要它!

我相信对于任何具有至少高于平均 SQL 技能的人来说,这都是一个非常简单的问题。我希望有这样的人出现:) 提前致谢!

SQL queries are still my weakest point, so here I am with yet another SQL question.

Imagine I have two tables: auctions and bids. Table auctions contains my auctions and table bids contains the list of bids for each auction.

Now I'm selecting values like this:

   SELECT
   `auction_title`,
   `auction_seo_title`,
   `auction_description_1`,
   `auction_unixtime_expiration`,
   `auction_startPrice`,
   MAX(`bids`.`bid_price`) as `bid_price`
   FROM
   `auctions`
   LEFT JOIN `bids` ON `auctions`.`auction_id`=`bids`.`bid_belongs_to_auction`
   ORDER BY
   `auction_unixtime_expiration`
   ASC
   LIMIT 5

The query works, but it's got a little catch to it: It selects only those auctions, which have at least one corresponding value inside the bids table. That means that if I have a new auction, which has no bids yet, the query doesn't return this auction, but I want it too!

I believe this is a very simple problem for anyone with at least above average SQL skills. I hope someone like that comes around :) Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

献世佛 2024-10-31 19:45:58
SELECT
   `auction_title`,
   `auction_seo_title`,
   `auction_description_1`,
   `auction_unixtime_expiration`,
   `auction_startPrice`,
   MAX(`bids`.`bid_price`) as `bid_price`
FROM
   `auctions`
LEFT JOIN `bids` ON `auctions`.`auction_id`=`bids`.`bid_belongs_to_auction`
GROUP BY `auction_id`
ORDER BY `auction_unixtime_expiration` ASC

尝试一下。假设可行,您可以将 LIMIT 添加到末尾。

SELECT
   `auction_title`,
   `auction_seo_title`,
   `auction_description_1`,
   `auction_unixtime_expiration`,
   `auction_startPrice`,
   MAX(`bids`.`bid_price`) as `bid_price`
FROM
   `auctions`
LEFT JOIN `bids` ON `auctions`.`auction_id`=`bids`.`bid_belongs_to_auction`
GROUP BY `auction_id`
ORDER BY `auction_unixtime_expiration` ASC

Give that a try. Assuming that works, you can add your LIMIT on to the end.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文