我对以下mySQL查询有问题,当引入最大日期时,它失败,如下所示。 我收到以下错误
错误代码:1054。“ where”中的未知列“ order_items.ORDER_ITEM_ID” 条款”
SET @UserID = 160;
SET @OrderDateTime = '2018-11-13 09:23:45';
SELECT
order_items.ORDER_ID,
listing_region.LIST_REGION_REGION_ID,
listings.LISTING_ID,
order_items.ORDER_REQUIRED_DATE_TIME,
listings.LISTING_NICK_NAME,
order_items.ORDER_QUANTITY,
order_price.ORDER_PRICE_ID,
order_items.ORDER_PORTION_SIZE,
t.LATEST_DATE,
t.ORDER_STATUS
FROM order_status_change, order_items
INNER JOIN listings ON listings.LISTING_ID = order_items.ORDER_LISTING_ID
INNER JOIN listing_region ON listing_region.LIST_REGION_LISTING_ID = listings.LISTING_ID
INNER JOIN order_price ON order_price.ORDERP_ITEM_ID = order_items.ORDER_ITEM_ID
INNER JOIN
(
SELECT MAX(order_status_change.ORDER_STATUS_CHANGE_DATETIME) AS LATEST_DATE, order_status_change.ORDER_ITEM_ID, order_status_change.ORDER_STATUS
FROM order_status_change
WHERE order_status_change.ORDER_ITEM_ID = order_items.ORDER_ITEM_ID
) AS t ON order_status_change.ORDER_ITEM_ID = t.ORDER_ITEM_ID AND order_status_change.ORDER_STATUS_CHANGE_DATETIME = t.LATEST_DATE
WHERE ((order_items.ORDER_USER_ID = @UserID) AND DATE(order_items.ORDER_REQUIRED_DATE_TIME) = DATE(@OrderDateTime))
有帮助吗?
答案 0 :(得分:1)
这是一个简化的版本来说明问题。
DROP TABLE IF exists t,t1;
create table t (id int);
create table t1(id int,dt date);
insert into t values (1),(2);
insert into t1 values (1,'2018-01-01'),(1,'2018-02-01'),(2,'2018-01-01');
select t.*,t2.maxdt
from t
join (select max(dt) maxdt,t1.id from t1 where t1.id = t.id) t2
on t2.id = t.id;
ERROR 1054 (42S22): Unknown column 't.id' in 'where clause'
您可以在子查询中分组,然后on子句将起作用
select t.*,t2.maxdt
from t
join (select max(dt) maxdt,t1.id from t1 group by t1.id) t2
on t2.id = t.id;
+------+------------+
| id | maxdt |
+------+------------+
| 1 | 2018-02-01 |
| 2 | 2018-01-01 |
+------+------------+
2 rows in set (0.00 sec)
如果您想要更接近问题的答案,请向sqlfiddle中添加示例数据和期望的输出作为问题的文本。
答案 1 :(得分:1)
我假设您可以加入order_status_change on order_items.ID = order_status_change.ORDER_ITEM_ID
如果这是有效的,那么我认为这将达到您追求的目标:
SET @UserID = 160;
SET @OrderDateTime = '2018-11-13 09:23:45';
SELECT
order_items.ORDER_ID
, listing_region.LIST_REGION_REGION_ID
, listings.LISTING_ID
, order_items.ORDER_REQUIRED_DATE_TIME
, listings.LISTING_NICK_NAME
, order_items.ORDER_QUANTITY
, order_price.ORDER_PRICE_ID
, order_items.ORDER_PORTION_SIZE
, t.LATEST_DATE
, order_status_change.ORDER_STATUS
FROM order_items
INNER JOIN listings ON listings.LISTING_ID = order_items.ORDER_LISTING_ID
INNER JOIN listing_region ON listing_region.LIST_REGION_LISTING_ID = listings.LISTING_ID
INNER JOIN order_price ON order_price.ORDERP_ITEM_ID = order_items.ORDER_ITEM_ID
INNER JOIN order_status_change ON order_items.ID = order_status_change.ORDER_ITEM_ID
INNER JOIN (
SELECT
MAX( mc.ORDER_STATUS_CHANGE_DATETIME ) AS LATEST_DATE
, mc.ORDER_ITEM_ID
FROM order_status_change AS mc
GROUP BY
mc.ORDER_ITEM_ID
) AS t
ON order_status_change.ORDER_ITEM_ID = t.ORDER_ITEM_ID
AND order_status_change.ORDER_STATUS_CHANGE_DATETIME = t.LATEST_DATE
WHERE order_items.ORDER_USER_ID = @UserID
AND DATE( order_items.ORDER_REQUIRED_DATE_TIME ) = DATE( @OrderDateTime )
您将来需要避免这种情况:
FROM order_status_change , order_items
两个表名之间的逗号 IS 是一个联接,但这是来自较旧的语法,并且优先级比查询的其他联接低。同样,默认情况下,这种基于逗号的联接相当于cross join
的乘积,该乘数乘以行数。简而言之,请不要在表名之间使用逗号。
另一个问题是,您缺少一个group by
子句,并且我相信您只是想从此聚合中获取“最新”日期,一旦确定该日期,就可以链接回该表以获取与之相关的状态那个日期。 (即,您无法在子查询中按状态分组,否则您将获得最新的dateS(每个状态一个)。