sql having get only the first recorded row in table and i want all

前端 未结 2 1584
时光说笑
时光说笑 2021-01-24 15:27

Somebody already helped me with this query but I made an adaptation and I get a problem :

    SELECT 
        AVG(tyd.price) AS avg_price, COUNT(tyd.id_product)          


        
相关标签:
2条回答
  • 2021-01-24 16:08

    The problem is that your query is non deterministic. You are selecting more columns that you are grouping by where the additional columns are not determined by the columns you are grouping by. If the latter had been true, then this would fall into the ANSII standards of SQL, since it is not it is (in my opinion) a failure of MySQL that it allows the statement to run at all. Other DBMS have gone the opposite way, since they cannot determine if certain columns are functions of other columns not contained in the group by, they will now allow any statement with columns in the select list that are not contained within the group by.

    To try and simplify the problem take the following dataset (Table T)

    ID    Col1    Col2
    1     1       1
    2     1       3
    

    Running this:

    SELECT Col1, MAX(Col2) AS MaxCol2, MIN(Col2) AS MinCol2, AVG(Col2) AS AvgCol2
    FROM T
    GROUP BY Col1
    

    Will always return

    Col1    MaxCol2    MinCol2    AvgCol2
    1       3          1          2
    

    However, if you throw ID into the mix

    SELECT ID, Col1, MAX(Col2) AS MaxCol2, MIN(Col2) AS MinCol2, AVG(Col2) AS AvgCol2
    FROM T
    GROUP BY Col1
    

    There is no way of determining which ID will be returned, 1 or 2, the most likely result is

    ID    Col1    MaxCol2    MinCol2    AvgCol2
    1    1       3          1          2
    

    However there is nothing defined in the SQL to state that the result could not be:

    ID    Col1    MaxCol2    MinCol2    AvgCol2
    2     1       3          1          2
    

    So if if the above was the result set, and you added HAVING ID = 1 to the query, you would get no results due to the point at which the HAVING clause is applied to the data. If you were to Add ID to the GROUP BY you would end up with 2 rows, which as I understand it is not what you want, and if you were to add it to the WHERE your MIN, MAX and AVG functions would be affected. So you need to use a subquery. So in this example I would use

    SELECT  T.ID, T.Col1, MaxCol2, MinCol2, AvgCol2
    FROM    T
            INNER JOIN
            (   SELECT Col1, MAX(Col2) AS MaxCol2, MIN(Col2) AS MinCol2, AVG(Col2) AS AvgCol2
                FROM T
                GROUP BY Col1
            ) T2
                ON T.Col1 = T2.Col1
    WHERE   ID = 1 -- OR ID = 2 DEPENDING ON REQUIREMENTS
    

    To apply this to your situation, the database engine has deteremined that the row that will be returned for the columns not in the group by is the row containing ID = 29. So your HAVING Clause is only being applied to this row, the rows with user1@tyd.fr, and user10@tyd.fr in have already been removed from the results by the time your HAVING clause is applied. You need to perform the aggregate functions separately to the filtering.

    Now I haven't fully got my head around your schema, but I am hoping I have explained the issue of the non deterministic statement well enough that you can make any ammendments required to my attempt at rewriting your query

    SELECT  Avg_Price,
            Cnt,
            tyd.id_marchand, 
            tyd.id_product, 
            catalog.price AS c_price, 
            tyd.price AS t_price, 
            tyd.amount AS t_am, 
            pro_tyd.amount AS p_am, 
            pro_tyd.price AS p_price, 
            catalog.img_src,  
            tyd.step, 
            tyd.login AS tyd_l
    FROM    Catalog
            INNER JOIN tyd
                ON catalog.id_marchand = tyd.id_marchand 
                AND catalog.id_product = tyd.id_product
            INNER JOIN Pro_tyd
                ON tyd.id_marchand = pro_tyd.id_marchand 
                AND tyd.id_product = pro_tyd.id_product
            INNER JOIN
            (   SELECT  ID_Marchand, ID_Product, Step, AVG(tyd.price) AS avg_price, COUNT(tyd.id_product) AS cnt
                FROM    Tyd
                WHERE   Step = '1'
                GROUP BY ID_Marchand, ID_Product, Step
            ) Agg
                ON Agg.id_marchand = pro_tyd.id_marchand 
                AND Agg.id_product = pro_tyd.id_product
                AND Agg.Step = tyd.Step
    WHERE   tyd.Login = 'user1@tyd.fr'
    
    0 讨论(0)
  • 2021-01-24 16:16

    i just want to add that this query :

    SELECT * FROM  tyd WHERE step = "1" 
    GROUP BY tyd.id_product, tyd.id_marchand 
    HAVING tyd.login = "user1@tyd.fr" 
    

    Has the same problem. Works when tyd.login = "user3" but not when an other user is ask...

    0 讨论(0)
提交回复
热议问题