1
    SELECT `bid_amount`,`valid_until`, min('bid_amount') as `lowest_bid`
    `cf1`.`country_name` AS `country_from`, `rq`.`city_from`,
    `cf2`.`country_name` AS `country_to`, `rq`.`city_to`,
    `sub_cat`.`sub_cat_name`,
    `cat`.`cat_name`
    FROM `bids`
    JOIN `request_quote` `rq` ON `bids`.`bid_for` = `rq`.`quoteid`
    JOIN `countries` `cf1` ON `rq`.`country_from` = `cf1`.`country_id`
    JOIN `countries` `cf2` ON `rq`.`country_to` = `cf2`.`country_id`
    LEFT JOIN `cat` ON `rq`.`cat` = `cat`.`cat_id`
    LEFT JOIN `sub_cat` ON `rq`.`sub_cat` = `sub_cat`.`sub_cat_id`
    WHERE `bids`.`bid_by` = $bidderId GROUP BY `bids`.`bid_amount`

所以我正在开发这种拍卖网站。我需要返回的是特定用户的所有出价,此外还有一些关于每个出价的额外信息。

我想要的一条信息是每件商品的最低出价。所以这里是出价表。'bid_for` 引用出价的项目。所以我想得到由“where bid_for = x”引用的'min(bid_amount)'。但正如您在最后一行中看到的,我已经有 WHERE 子句通过用户 ID 引用它。

所以简而言之,是否有选择一个(或多个)由不同“where”子句引用的信息而不是查询的其余部分?(如果这有意义的话)

+--------+--------+---------+------------+-------------+---------------+-------------+
| bid_id | bid_by | bid_for | bid_amount | pickup_date | delivery_date | valid_until |
+--------+--------+---------+------------+-------------+---------------+-------------+
|      1 |     24 |       2 |      19.99 | 2013-06-01  | 2013-06-01    | 2013-06-01  |
|      2 |     27 |       2 |      25.00 | 2013-06-01  | 2013-06-01    | 2013-06-01  |
+--------+--------+---------+------------+-------------+---------------+-------------+
4

2 回答 2

1

您可以使用内部选择语句执行此操作:

SELECT `bid_amount`,`valid_until`, (SELECT min('bid_amount') FROM 'bids') as `lowest_bid`
`cf1`.`country_name` AS `country_from`, `rq`.`city_from`,
`cf2`.`country_name` AS `country_to`, `rq`.`city_to`,
`sub_cat`.`sub_cat_name`,
`cat`.`cat_name`
FROM `bids`
JOIN `request_quote` `rq` ON `bids`.`bid_for` = `rq`.`quoteid`
JOIN `countries` `cf1` ON `rq`.`country_from` = `cf1`.`country_id`
JOIN `countries` `cf2` ON `rq`.`country_to` = `cf2`.`country_id`
LEFT JOIN `cat` ON `rq`.`cat` = `cat`.`cat_id`
LEFT JOIN `sub_cat` ON `rq`.`sub_cat` = `sub_cat`.`sub_cat_id`
WHERE `bids`.`bid_by` = $bidderId GROUP BY `bids`.`bid_amount`
于 2013-06-14T00:38:09.590 回答
0

好的,我自己想出了解决方案。我给表加上了别名bids,并将它加入到没有别名的bids表中。像这样:

SELECT `bids`.`bid_amount`,`bids`.`valid_until`,
    min(`min`.`bid_amount`) AS `lowest_bid`,
    `cf1`.`country_name` AS `country_from`, `rq`.`city_from`,
    `cf2`.`country_name` AS `country_to`, `rq`.`city_to`,
    `sub_cat`.`sub_cat_name`,
    `cat`.`cat_name`
    FROM `bids`
    JOIN `request_quote` `rq` ON `bids`.`bid_for` = `rq`.`quoteid`
    JOIN `bids` `min` ON `min`.`bid_for` = `rq`.`quoteid`
    JOIN `countries` `cf1` ON `rq`.`country_from` = `cf1`.`country_id`
    JOIN `countries` `cf2` ON `rq`.`country_to` = `cf2`.`country_id`
    LEFT JOIN `cat` ON `rq`.`cat` = `cat`.`cat_id`
    LEFT JOIN `sub_cat` ON `rq`.`sub_cat` = `sub_cat`.`sub_cat_id`
    WHERE `bids`.`bid_by` = $bidderId GROUP BY `bids`.`bid_for`

甚至不知道您可以将同一张桌子加入同一张桌子。但在这里,你去,生活和学习:)

于 2013-06-14T12:57:05.580 回答