Query two db using group by and display detail information

后端 未结 1 864
渐次进展
渐次进展 2021-01-26 13:55

I have two tables
1: itemInfo

product_id  brand   name    category 
1   Garden  Goodcake    cake

2: itemRecord

record_id sh         


        
相关标签:
1条回答
  • 2021-01-26 13:57

    JOIN them:

    SELECT r.product_id, i.brand, i.name, i.category, DATE_FORMAT( r.inputTime, '%e-%b' ) AS inputTime, r.shopType, r.price AS minimum_price, r.record_id
    FROM (  SELECT *
            FROM itemRecord
            WHERE product_id = '1'
            ORDER BY price ASC, inputTime DESC) AS r
    INNER JOIN itemInfo As i
    ON r.product_id = i.product_id
    WHERE i.id = r.product_id
    GROUP BY DATE(r.inputTime)
    LIMIT 0, 7
    

    Explanation:

    I do an inner query which renders the table order by price ASC instead of the default, lets say id ASC. When you GROUP BY the rows, it uses the columns from the first row by default, which in this case is the one with lowest price.

    Your solution didn't work since it might aswell have chosen the first id and not the lowest price row. The only column which was correct was the MIN( r.price ), but as you noticed that function did not affect the other columns in the result.

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