简体   繁体   English

SQL查询提取具有相同WHERE条件的硬编码记录和金额

[英]SQL query pulling hardcoded records and amounts all with the same WHERE conditions

I have a car database and I am trying to pull one of each make and model of cars. 我有一辆车数据库,我试图拉每个品牌和汽车型号之一 Example: There are three Toyota Corollas, four Honda Civics and five Acura RSX. 示例:有三辆丰田卡罗拉,四辆本田思域和五辆ura歌RSX。 The query should return one Toyota Corolla, one Honda Civic and one Acura RSX. 该查询应返回一辆Toyota Corolla,一辆Honda Civic和一辆Acura RSX。 I can code this however there is a lot of repetition of code in the query. 我可以对此进行编码,但是查询中有很多重复的代码。

    SELECT
        *
    FROM
        main
    WHERE
        ( make_name = 'TOYOTA' AND model_name = 'CELICA' AND chassis = 'GT4' )
        AND
        mileage <= 100000
        AND
        rate >= '3.5'
        AND
        ( year >= 1990 and year <= 1998 )
    ORDER BY
        auction_date DESC,
        rate DESC
    LIMIT
        1
)
UNION
(
    SELECT
        *
    FROM
        main
    WHERE
        ( make_name = 'NISSAN' AND model_name = 'SKYLINE' AND chassis = 'R33' )
        AND
        mileage <= 100000
        AND
        rate >= '3.5'
        AND
        ( year >= 1990 and year <= 1998 )
    ORDER BY
        auction_date DESC,
        rate DESC
    LIMIT
        1
)

So this will pull one Toyota Celica GT4 and one Nissan Skyline R33. 因此,这将拉动一辆丰田Celica GT4和一辆日产Skyline R33。 But I repeat the conditions of mileage, rate and year for each car. 但是我重复每辆车的里程,费率和年份的条件。 This query has two cars I need but essentially I will be adding more in over time so I would like a way to add to this query without repeating the WHERE condition for each car since they will always be the same. 该查询有我需要的两辆车,但是随着时间的推移,我基本上会增加更多的车,所以我想一种添加到该查询中的方法,而不必为每辆车重复WHERE条件,因为它们总是一样的。

SQL Statement SQL语句

select 
*
from
(
SELECT make_name,
model_name,
chassis,
max(concat(auction_date,rate)) as mc
from main 
where
mileage <= 100000
        AND
        rate >= '3.5'
        AND
        ( year >= 1990 and year <= 1998 )  
group by
make_name, model_name, chassis 

) as a,
main as m
where a.make_name =m.make_name AND
a.model_name = m.model_name AND
a.chassis = m.chassis AND
a.mc = concat(m.auction_date,m.rate);

Explanation 说明

This uses a derived table containing the distinct combinations of make_name , model_name , and chassis , as well as the MAXIMUM auction_date+rate . 这使用含的不同组合派生表make_namemodel_name ,以及chassis ,以及所述MAXIMUM auction_date+rate

I use the MAXIMUM auction_date+rate since we have the requirement is to get a only one ROW for each make_name, model_name, and chassis, but to get that row that has the LATEST auction_date, and highest rate. 我使用MAXIMUM auction_date+rate因为我们的要求是每个make_name,model_name和底盘仅获得一个ROW,但是要获得具有最新的Auction_date和最高利率的行。

This produces the derived table with results: 这将生成带有结果的派生表:

MAKE_NAME      MODEL_NAME   CHASSIS         MC
NISSAN         SKYLINE       GT4        0005-06-0117
NISSAN         SKYLINE       R33        0002-04-011
TOYOTA         CELICA        GT4        0005-01-0117

I then join this derived table with the main table, but with the conditions: 然后,我将此派生表与主表联接在一起,但条件如下:

where a.make_name =m.make_name AND
a.model_name = m.model_name AND
a.chassis = m.chassis AND
a.mc = concat(m.auction_date,m.rate);

In this way, the rows are extracted that have the make, model, and chassis with the latest aucion date, and highest rate (But only one row for each make, model, and chassis). 这样,将提取行,具有最新拍卖日期和最高利率的品牌,型号和底盘的行(但每个品牌,型号和底盘仅一行)。

SQLFiddle: http://sqlfiddle.com/#!2/1b5c8/2 SQLFiddle: http ://sqlfiddle.com/#!2/1b5c8/2

Update 更新

I want to specify in the SQL which unique models I want. 我想在SQL中指定所需的唯一模型。 Which was why I went: WHERE model_name='GT4' etc.. 这就是为什么我去:WHERE model_name ='GT4'等。

You can do this using multiple OR statements or using CONCAT and IN (or simply IN ). 您可以使用多个OR语句或使用CONCATIN (或简单地是IN )来执行此操作。

So, you either append at the end: 因此,您可以在末尾追加:

 AND 
 (
  (m.model_name='CELICA' AND m.chassis='GT4') OR
  (m.model_name='TEST' AND m.chassis='TEST')
);

or you append: 或者您附加:

AND concat(m.model_name,'-',m.chassis) in ('CELICA-GT4','SKYLINE-GT4');

See: http://sqlfiddle.com/#!2/6fc09/2 that has both examples. 请参阅: http//sqlfiddle.com/#!2 / 6fc09 / 2 ,其中包含两个示例。

Because you are using a LIMIT clause to restrict the number of rows returned, and because the LIMIT clause gets evaluated last (or very nearly last) in the execution plan, the WHERE clause gets evaluated before the LIMIT , so that WHERE clause has to be repeated in each query. 由于您使用的是LIMIT子句限制的行数返回,因为LIMIT条款得到最后一个(或非常接近最后的)在执行计划进行评价的WHERE子句中获取之前评估LIMIT ,使WHERE子句必须是在每个查询中重复。

There are other approaches to returning an equivalent resultset which would avoid repeating some of the conditions in the where clause, but they won't be as efficient, and they won't be able to make use of the ORDER BY ... LIMIT ... construct to restrict the rows returned. 还有其他返回相等结果集的方法,这些方法可以避免重复where子句中的某些条件,但是它们的效率不高,并且无法使用ORDER BY ... LIMIT ...构造以限制返回的行。

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

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