-2

你好亲爱的SO的朋友们。

我有这个表:

宝仕奥

+---------+--------+-----------+-------------+------------+
| priceID | itemID | priceCash | pricePoints | priceDate  |
+---------+--------+-----------+-------------+------------+
|       1 |      1 |     30.00 |          90 | 2012-03-05 |
|       2 |      2 |     40.00 |         120 | 2012-03-05 |
|       6 |      2 |     50.00 |          50 | 2012-03-07 |
|       7 |      2 |     55.00 |          50 | 2012-03-07 |
+---------+--------+-----------+-------------+------------+

项目

+--------+----------------------+-------------+------------+----------+---------------+---------------+
| itemID | itemName             | itemWarning | itemUrgent | itemType | itemVipPoints | itemExistence |
+--------+----------------------+-------------+------------+----------+---------------+---------------+
|      1 | Lubricante Orgasmix  |          20 |         10 |        2 |             3 |           300 |
|      2 | Anillo Vibrador      |          50 |         50 |        2 |            50 |           120 |
|      3 | Crema Chantilly      |           5 |         20 |        1 |             0 |           500 |
|      4 | Caribe Cooler        |          10 |          4 |        1 |             0 |           100 |
|      5 | Cacahuates Japoneses |          20 |         10 |        1 |             0 |           400 |
|      6 | Cerveza Sol (lata)   |          12 |        112 |        1 |             0 |           200 |
|      7 | Chocolate derretido  |          20 |         10 |        1 |             0 |           200 |
+--------+----------------------+-------------+------------+----------+---------------+---------------+

我需要一张这样的桌子:

  • itemType 必须为“2”
  • itemID 是必填项
  • 项目名称是必需的
  • priceCash 是必需的
  • itemExistence 必须 > 0

但是,主要问题(对我来说)是我需要为每个独特的项目获取最新的 priceCash。

例如,如您所见,itemID = 2 具有三个价格。在这种情况下,我需要表格只显示最后一个(55.00)。

所以,简而言之:我需要得到这个:

+--------+---------------------+-----------+
| itemID | itemName            | priceCash |
+--------+---------------------+-----------+
|      1 | Lubricante Orgasmix |     30.00 |
|      2 | Anillo Vibrador     |     55.00 |
+--------+---------------------+-----------+

但我最好的结果是这样的:(

+--------+---------------------+-----------+
| itemID | itemName            | priceCash |
+--------+---------------------+-----------+
|      1 | Lubricante Orgasmix |     30.00 |
|      2 | Anillo Vibrador     |     40.00 |
|      2 | Anillo Vibrador     |     50.00 |
|      2 | Anillo Vibrador     |     55.00 |
+--------+---------------------+-----------+

我将在4 月 15 日通过 Paypal 向可以帮助我的人支付 5 美元小费 :) 承诺。

解决方案

kaj友情提供

select it.itemID, it.itemName, p.priceCash
from items it
  inner join precios p on p.itemID = it.itemID
  inner join (select itemID, max(priceID) latestPriceID
              from precios
              group by itemID) latestPrice on latestPrice.itemID = p.itemID and latestPrice.latestPriceID = p.priceID
where it.itemType = 2
  and it.itemExistence > 0

对于那些介意的人来说,这是关于汽车旅馆的数据库,他们在那里出售各种东西以获得性快感。

谢谢。

4

2 回答 2

2

鉴于您有重复priceDatesfor itemID= 2 我假设最新价格可以通过使用priceID. 所以基本上你需要一个查询来找到每件商品的最新价格点,然后提取当时的相关价格。

像下面这样的东西应该可以工作:

select it.itemID, it.itemName, p.priceCash
from items it
  inner join precios p on p.itemID = it.itemID
  inner join (select itemID, max(priceID) latestPriceID
              from precios
              group by itemID) latestPrice on latestPrice.itemID = p.itemID and latestPrice.latestPriceID = p.priceID
where it.itemType = 2
  and it.itemExistence > 0
于 2012-04-11T18:56:44.757 回答
-1
 select x.itemID, x.itemName, y.priceCash from items x, precios y
 WHERE itemType=2 and x.itemID=y.itemID and x.itemExistence > 0 
 and y.priceID = (select max(t.priceID) from precios t where x.itemID=t.itemID)
 group by x.itemID order by y.priceDate;
于 2012-04-11T18:58:12.290 回答