1

我有两个表,mytableA 和 mytableB。

MytableA 看起来像这样

ColumnA|Columnb
Apple|fruitstand
Pear|fruitstand

mytableB 看起来像这样:

A列|B列|C列
苹果|2|2012 年 4 月 1 日
苹果|3|2012 年 4 月 3 日
苹果|23|12/1/2011
梨|1|12/22/2011
梨|0|2012/4/22

我的查询是这个

select * 
from 
  mytableB 
  join mytableB on mytableA.ColumnA = mytableB.ColumA
     and mytableB.Columnc = (select max(Columnc) from mytableB b where b.ColumnA = mytableB.columnA)

我正在寻找的结果是

苹果|3|2012 年 4 月 3 日
梨|0|2012/4/22

我知道这在 SQL 中有效,但 mysql 有类似的方法吗?

4

1 回答 1

0

我试过了。它在 mysql 上有效(不包括大量拼写错误),但结果对我来说没有任何意义。

ColumnA | ColumnB       | ColumnA | ColumnB | ColumnC
Apple     fruitstand      Pear      0         4/22/2012
Pear      fruitstand      Pear      0         4/22/2012

编辑:您应该使用某种索引来识别条目。

像 mytablea

ID | name | type

我的表b

ID | fruitID | stock | date

然后

SELECT mytablea.name, mytableb.stock, mytableb.date FROM mytablea, mytableb, 
       (SELECT MAX(mytableb.date) as maxDate FROM mytableb, mytablea 
       WHERE mytableb.fruitID = mytablea.ID GROUP BY mytablea.ID) AS maxTB 
       WHERE mytableb.date = maxTB.maxDate AND mytablea.ID = mytableb.fruitID

应该做的伎俩......这是一些蹩脚的大子查询:-!

结果:

name | stock | date
Apple  4       4/3/2012
Pear   0       4/22/2012
于 2012-05-11T02:24:44.490 回答