为什么我不能加入这个查询与最大日期?

ktecyv1j  于 2021-06-17  发布在  Mysql
关注(0)|答案(2)|浏览(288)

我对下面的mysql查询有一个问题,它在引入max date时失败,如下所示。我得到以下错误
错误代码:1054。“where子句”中的未知列“order\u items.order\u item\u 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))

有什么帮助吗?

jhiyze9q

jhiyze9q1#

我以为你能加入 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

两个表名之间的逗号是一个联接,但它来自较旧的语法,并且它的优先级低于查询的其他联接。另外,默认情况下,这个基于逗号的连接相当于 cross join 它乘以行数。简而言之,请不要在表名之间使用逗号。
另一个问题是你错过了 group by 子句,我相信您只想从这个聚合中获取“最新”日期,一旦确定了这个日期,就链接回该表以获取与该日期相关的状态(i、 不能在子查询中按状态分组,否则会得到最新的日期(每个状态对应一个日期)。

xkftehaa

xkftehaa2#

这里有一个简化的版本来说明这个问题。

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)

如果您想得到更接近您的问题的答案,请将示例数据和预期输出作为SQLFIDLE的文本添加到问题中。

相关问题