我有三个名为tblStock,tblModel和tblAltStockPrice的表。
tblStock:
|------------------------------------------------------|
| StockCode | Description | StockModel |
|-------------------------|----------------------------|
| 1Y 1111 | ORING | CATERPILLAR 950 LOADER |
|-------------------------|----------------------------|
| 2K 4501 | BUSHING | CATERPILLAR 950 LOADER |
|------------------------------------------------------|
tblModel:
|------------------------------------|
| ModelCode | ModelDescription |
|------------------------------------|
| C950 | CATERPILLAR 950 LOADER |
|------------------------------------|
tblAltStockPrice:
|-----------------------------------------------------------|
| StockCode | Price | UpdateTime | UpdateDate |
|-------------------------|-----------------|---------------|
| 1Y 1111 | 40 | 12:52:41 | 30/12/2013 |
|-------------------------|-----------------|---------------|
| 1Y 1111 | 45 | 12:11:17 | 30/12/2013 |
|-------------------------|-----------------|---------------|
| 2K 4501 | 40 | 12:13:14 | 30/12/2013 |
|-----------------------------------------------------------|
问题:
我希望检索连接这三个表的MAX(UpdateTime)和MAX(UpdateDate)记录。
我的查询是:
SELECT * FROM tblStock s
INNER JOIN tblModel m ON s.StockModel = m.ModelDescription
INNER JOIN tblAltStockPrice asp ON s.StockCode = asp.StockCode
WHERE m.ModelCode='C950'
ORDER BY MAX(updateTime) AND MAX(updateDate)
输出结果为:
|----------------------------------------------------------------------------------------------------------------------|
| StockCode | Description | ModelCode | ModelDescription | Price | UpdateTime | UpdateDate |
|------------------------------|----------------|--------------------------|---------|-----------------|---------------|
| 1Y 1111 | ORING | C950 | CATERPILLAR 950 LOADER | 40 | 12:52:41 | 30/12/2013 |
|----------------------------------------------------------------------------------------------------------------------|
我的预期输出是:
|----------------------------------------------------------------------------------------------------------------------|
| StockCode | Description | ModelCode | ModelDescription | Price | UpdateTime | UpdateDate |
|------------------------------|----------------|--------------------------|---------|-----------------|---------------|
| 1Y 1111 | ORING | C950 | CATERPILLAR 950 LOADER | 40 | 12:52:41 | 30/12/2013 |
|-----------|------------------|----------------|--------------------------|---------|-----------------|---------------|
| 2K 4501 | BUSHING | C950 | CATERPILLAR 950 LOADER | 40 | 12:13:14 | 30/12/2013 |
|----------------------------------------------------------------------------------------------------------------------|
任何人都可以纠正我的错误吗? 谢谢。
答案 0 :(得分:4)
您需要一个GROUP BY
子句,以便MAX
按股票代码聚合,而不是聚合所有行。此外,多个排序条件应使用逗号分隔,而不是AND
。最后,在您的订购中,日期应该优先于时间。
SELECT * FROM tblStock s
INNER JOIN tblModel m ON s.StockModel = m.ModelDescription
INNER JOIN tblAltStockPrice asp ON s.StockCode = asp.StockCode
WHERE m.ModelCode='C950'
GROUP BY s.StockCode
ORDER BY MAX(updateDate), MAX(updateTime)
答案 1 :(得分:0)
在表LEFT JOIN
和tblStock
tblModel