简体   繁体   English

SQL连接与其他查询不同的“ where”子句?

[英]SQL join with different “where” clause than the rest of the query?

    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`

So I am working on this auction kind of site. 所以我正在这个拍卖网站上工作。 What I need to return is all bids by particular user, and in addition to that some extra information about every bid. 我需要返回的是特定用户的所有出价,以及有关每个出价的一些额外信息。

One piece of information that I want is the lowest bid on every item. 我想要的一条信息是每个项目的最低出价。 So here is the bids table. 这是出价表。 'bid_for` references the item for which the bid is. “ bid_for”引用要出价的项目。 So I want to get that 'min(bid_amount)' that is referenced by "where bid_for = x". 因此,我想获取由“ where bid_for = x”引用的“ min(bid_amount)”。 But as you see in the last line I already have WHERE clause that references it by user id. 但是,正如您在最后一行看到的那样,我已经有了WHERE子句,该子句通过用户ID对其进行引用。

So in short, is there anyway to select one (or more) piece of information that is referenced by different "where" clause than the rest of the query? 简而言之,是否有选择与查询其余部分不同的“ where”子句引用的一条(或多条)信息? (if that makes sense) (如果有道理)

+--------+--------+---------+------------+-------------+---------------+-------------+
| 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  |
+--------+--------+---------+------------+-------------+---------------+-------------+

You can do this with an inner select statement: 您可以使用内部选择语句执行此操作:

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`

Ok came up with solution myself. 好的,我自己想出了解决方案。 I aliased the bids table, and joined it to the un-aliased bids table. 我为bids表加上了别名,然后将其加入到没有别名的bids表中。 Like so: 像这样:

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`

Didn't even know you could join same table onto same table. 甚至都不知道您可以将同一张表连接到同一张表上。 But here you go, live and learn :) 但是,您可以在这里生活和学习:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM