我怎样才能在MySQL中实现这个查询

时间:2012-04-11 18:45:28

标签: mysql join

你好亲爱的朋友们。

我有这张表:

PRECIOS

+---------+--------+-----------+-------------+------------+
| 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是必需的
  • itemName是必需的
  • 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日víaPaypal向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

对于那些介意的人来说,它是关于一个汽车旅馆的数据库,他们出售各种性快感的东西。

感谢。

2 个答案:

答案 0 :(得分:2)

鉴于您priceDates = 2的重复itemID,我认为可以使用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

答案 1 :(得分:-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;