在mysql查询中选择最小值时获取行详细信息

时间:2016-09-14 15:00:48

标签: mysql

我正在使用以下查询,选择数据库中的最低价格以及所需的所有其他数据。

SELECT 
    p1.id as pid, p1.persons, p1.skipas_incl, temp.* 
FROM
    winter_accommodaties_prijzen p1
INNER JOIN (
    SELECT
        a.id as accom_id, p.vertrekdatum, Min(p.prijs) as prijs, a. max_personen, a.naam as naam, g.pistes_groen+g.pistes_blauw+g.pistes_rood+g.pistes_zwart as pistes, liften_type1+liften_type2+liften_type3+liften_type4 as liften, g.naam as gebied, g.lid, d.gid, d.naam as dorp, d.hoogte_dorp, d.afstand_utrecht, d.dalafdaling, g.min_hoogte, g.max_hoogte, t.naam as typenaam, afstand_piste
    FROM
        winter_accommodaties_prijzen p
        INNER JOIN winter_accommodaties a ON (a.id = p.accid)
        INNER JOIN winter_dorpen d ON (d.id = a.did)
        INNER JOIN winter_gebied g ON (g.id = d.gid)
        INNER JOIN winter_accommodaties_types t ON (a.tid = t.id)
    WHERE
        g.lid IN(1,2) AND (p.vertrekdatum BETWEEN '2016-08-30' AND '2017-06-30' OR p.vertrekdatum = '0000-00-00') AND g.max_hoogte >= 500 AND (g.pistes_groen+g.pistes_blauw+g.pistes_rood+g.pistes_zwart) >= 50 AND d.hoogte_dorp >= 500 AND d.afstand_utrecht <= 2500 AND prijs < 1200 AND p.persons >= 4
    GROUP BY
        p.accid
    ORDER BY 
        prijs, naam ASC
    LIMIT
        0, 10) AS temp
ON 
    temp.accom_id = p1.accid AND 
    temp.prijs = p1.prijs AND
    temp.vertrekdatum = p1.vertrekdatum
GROUP BY
    p1.accid

括号中的SELECT选择所需的各种字段和最低价格。 'vertrekdatum'是父查询中用于获取正确price_id的日期。这作为价格/住宿可以有多个休假日期,因此

之间的组合
price<->ID<->date

目前的问题是,选择的日期(以及可能的其他字段)也不会与具有MIN(p.prijs)的行持久链接,从而导致父查询中的错误,例如它只显示一个意外正确的选择。

我正在努力解决这个问题,因为我已经尝试过表之间的各种选项/联系,但是没有得到预期的结果。你们可以帮我提一下如何改进这个查询,以便提供预期的结果吗?

解决方案

经过一些测试,玩耍和大量搜索后,以下是对我有用的结果:

SELECT              
    subset.*, a.max_personen, a.naam as naam
FROM                
    (
    SELECT
        temp.*,
        @rowNum := if(@prevValue = temp.accom_id, @rowNum+1, 1) as row_num, 
        @prevValue := temp.accom_id as accid1
    FROM
        (
        SELECT
            p.id as pid, p.accid as accom_id, p.prijs, p.vertrekdatum,   p.persons, p.skipas_incl, g.pistes_groen+g.pistes_blauw+g.pistes_rood+g.pistes_zwart as pistes, g.liften_type1+g.liften_type2+g.liften_type3+g.liften_type4 as liften, g.naam as gebied, g.lid, d.gid, d.naam as dorp, d.hoogte_dorp, d.afstand_utrecht, d.dalafdaling, g.min_hoogte, g.max_hoogte, t.naam as typenaam, a.afstand_piste                    
        FROM                        
            winter_accommodaties_prijzen p                      
            INNER JOIN winter_accommodaties a ON (a.id = p.accid)                       
            INNER JOIN winter_dorpen d ON (d.id = a.did)                        
            INNER JOIN winter_gebied g ON (g.id = d.gid)                        
            INNER JOIN winter_accommodaties_types t ON (a.tid = t.id)                   
        WHERE                       
            g.lid IN(1,2) 
            AND (p.vertrekdatum BETWEEN '2016-09-16' AND '2017-05-16' OR   p.vertrekdatum = '0000-00-00') 
            AND g.max_hoogte >= 500 
            AND (g.pistes_groen+g.pistes_blauw+g.pistes_rood+g.pistes_zwart) >= 50
            AND d.hoogte_dorp >= 500 
            AND d.afstand_utrecht <= 2500
            AND prijs < 1200 
            AND p.persons >= 4                  
        ORDER BY                         
            p.accid, p.prijs ASC, p.vertrekdatum) temp,             
        (SELECT @rowNum := 1 FROM DUAL) X,              
        (SELECT @prevValue := -1 FROM DUAL) Y) subset           
INNER JOIN winter_accommodaties a ON (a.id = subset.accom_id)           
WHERE               
    subset.row_num = 1           
ORDER BY 
    prijs, naam ASC         
LIMIT               
    0, 10

0 个答案:

没有答案