So I am having a slight issue. I have 2 tables, one contains the merchant information, name etc and another contains Events.
I upload prices to the event table every few days so there is a lot of duplicate content only distinguished through its TIMESTAMP column(Uploaddate).
I need to select the prices for the most recent uploads and therefore the others not to be selected.
Event name date price merchant test 2012-02-26 £10 ShopX test 2012-02-26 £11 ShopY test 2012-02-26 £9 ShopX - LATEST PRICE
IE. Select prices which are the most up-to-date and make sure each merchant is unique, so only 1 price per merchant.
Here is what I have so far:
SELECT *
FROM wp_events, wp_merchants
WHERE DATE = '2012-02-26'
AND eventname = 'testevent'
AND `uploaddate` > NOW( )
AND wp_events.merchant = wp_merchants.merchant_name
ORDER BY uploaddate DESC
Use MAX()
in a subquery, which will look something like this:
SELECT
MAX(DATE), event, merchant
FROM
someTable
GROUP BY
event, merchant
Next, incorporate the subquery into a main query to get the latest price (I guess that's what you want):
SELECT
someTable.price, someTable.event, someTable.merchant
FROM
someTable INNER JOIN (
SELECT
MAX(DATE) AS DATE, event, merchant
FROM
someTable
GROUP BY
event, merchant
) subquery
ON someTable.DATE = subquery.DATE AND someTable.event = subquery.event AND someTable.merchant = subquery.merchant
I haven't verified the code.
Give this a try:
select e1.merchant, e1.price from event e1
left join event e2
on (e1.merchant = e2.merchant and e1.date < e2.date)
where e2.merchant is null
This will get you this result:
+----------+-------+
| MERCHANT | PRICE |
+----------+-------+
| ShopY | £11 |
| ShopX | £9 |
+----------+-------+
So, you'll have to add the joins and filters you need.
Edit:
Isn't this what you're looking for?
select e1.merchant, e1.price from wp_events e1
left join wp_events e2
on (e1.merchant = e2.merchant and e1.uploaddate < e2.uploaddate)
where e2.merchant is null
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.