Note that there are some explanatory texts on larger screens.

plurals
  1. POQuery two db using group by and display detail information
    primarykey
    data
    text
    <p>I have two tables<br> 1: itemInfo</p> <pre><code>product_id brand name category 1 Garden Goodcake cake </code></pre> <p>2: itemRecord</p> <pre><code>record_id shopType price inputTime product_id 1 0 7.2 2012-03-20 17:58:56 1 69 2 6.4 2012-03-20 19:33:12 1 70 0 9.9 2012-03-19 00:00:00 1 71 3 5.5 2012-03-18 05:22:00 1 72 0 6.8 2012-03-19 20:26:29 1 73 0 10.1 2012-03-17 12:27:00 1 74 3 7.9 2012-03-16 00:00:00 1 75 1 6.7 2012-03-15 00:00:00 1 76 1 8.8 2012-03-14 11:24:00 1 77 2 12.5 2012-03-13 10:26:19 1 78 0 7.2 2012-03-21 11:25:23 1 146 2 5 2012-03-21 12:53:04 1 149 3 4.9 2012-03-21 10:00:00 1 150 3 5.1 2012-03-21 12:20:00 1 </code></pre> <p>I would like to display the latest 7 days of cheapest price of product_id = 1. If the same day, the cheapest price is same, just display the latest.<br> The final table should be: </p> <pre><code>product_id brand name category inputTime shopType minimum_price record_id 1 Garden Goodcake cake 2012-03-21 3 4.9 149 1 Garden Goodcake cake 2012-03-20 2 6.4 69 1 Garden Goodcake cake 2012-03-19 2 6.8 72 ... 1 Garden Goodcake cake 2012-03-15 1 6.7 75 </code></pre> <p>I have tried my best to write a sql that like: </p> <pre><code>SELECT r.product_id, i.brand, i.name, i.category, DATE_FORMAT( r.inputTime, '%e-%b' ) AS inputTime, r.shopType, MIN( r.price ) AS minimum_price, r.record_id FROM itemRecord AS r, itemInfo AS i WHERE r.product_id = '1' AND i.id = r.product_id GROUP BY DATE( r.inputTime ) DESC LIMIT 0 , 7 </code></pre> <p>However, the table is: </p> <pre><code>product_id brand name category inputTime shopType minimum_price record_id 1 Garden Goodcake cake 21-Mar 0 4.9 78 1 Garden Goodcake cake 20-Mar 0 6.4 1 1 Garden Goodcake cake 19-Mar 0 6.8 70 1 Garden Goodcake cake 18-Mar 3 5.5 71 1 Garden Goodcake cake 17-Mar 0 10.1 73 1 Garden Goodcake cake 16-Mar 3 7.9 74 1 Garden Goodcake cake 15-Mar 1 6.7 75 </code></pre> <p>For example, the first row result, it displays the correct minimum price 4.9, but the record_id is wrong, also, the shopType is wrong too.<br> I have research a lot, but still cannot solve the problem.<br> Please help...</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload