我最初没有编写这段代码,但它确实曾经有效。不幸的是它不再有效了。我的研究表明它在SQL类型中是不匹配的,但我在SQL中不够精通修复错误。希望有人可以指导我。
SELECT DISTINCT (`users`.`card_id` ) AS `card_id2`
, `users` .`firstname` AS `First Name`
, `users`.`lastname` AS `Last Name`
, `users`.`card_id` AS `Card ID`
, `users`.`address1` AS `Address`
, `users`.`address2` AS `Address2`
, `users`.`city` AS `City`
, `users`.`state` AS `State`
, `users`.`zip` AS `Zip`
, `users`.`email` AS `Email`
, `users`.`phone_area`
, `users`.`phone_exchange`
, `users`.`phone_number`
, `trans`.`trans_dt` AS `Last Trans Date`
, `trans`.`credit_balance` AS `Credit Balance`
, `trans`.`balance` AS `Gift Balance`
, `users`.`gender` AS `Gender`
, `users`.`birthdate` AS `Birthday (Age)`
, `users`.`register_dt` AS `Registration Date`
, `merchants`.`company`
, `merchants`.`merchantkey`FROM `trans`
, `users`
, (SELECT `card_id`, max( `trans_id` ) AS `trans_dt`
FROM `trans`
WHERE `trans`.`trans_type` >0 GROUP BY `card_id`
) AS `trans_max`
LEFT JOIN `merchants` ON `merchants`.`merchantkey`=`trans`.`merchantkey`
WHERE trans.card_id = users.card_id
AND trans.card_id = trans_max.card_id
AND trans.trans_id = trans_max.trans_dt
ORDER BY users.lastname, users.firstname;
Error Code: 1054. Unknown column 'trans.merchantkey' in 'on clause'
答案 0 :(得分:0)
我复制并粘贴您的查询。 FROM
子句位于错误的位置。 merchants.merchantkey
和FROM trans
之间没有空格。为什么在SELECT
子句之后还有一些FROM
子句,然后是LEFT JOIN
?
SELECT DISTINCT ...
, `merchants`.`company`
, `merchants`.`merchantkey`FROM `trans`
, `users`
, (SELECT `card_id`, max( `trans_id` ) AS `trans_dt`
FROM `trans`
WHERE `trans`.`trans_type` >0 GROUP BY `card_id`
) AS `trans_max`
LEFT JOIN `merchants` ON `merchants`.`merchantkey`=`trans`.`merchantkey`
...
我在想你的FROM
不合适。尝试在FROM
LEFT JOIN
子句
SELECT DISTINCT (`users`.`card_id` ) AS `card_id2`
, `users` .`firstname` AS `First Name`
, `users`.`lastname` AS `Last Name`
, `users`.`card_id` AS `Card ID`
, `users`.`address1` AS `Address`
, `users`.`address2` AS `Address2`
, `users`.`city` AS `City`
, `users`.`state` AS `State`
, `users`.`zip` AS `Zip`
, `users`.`email` AS `Email`
, `users`.`phone_area`
, `users`.`phone_exchange`
, `users`.`phone_number`
, `trans`.`trans_dt` AS `Last Trans Date`
, `trans`.`credit_balance` AS `Credit Balance`
, `trans`.`balance` AS `Gift Balance`
, `users`.`gender` AS `Gender`
, `users`.`birthdate` AS `Birthday (Age)`
, `users`.`register_dt` AS `Registration Date`
, `merchants`.`company`
, `merchants`.`merchantkey
, `users`
, (
SELECT `card_id`, max( `trans_id` ) AS `trans_dt`
FROM `trans`
WHERE `trans`.`trans_type` >0 GROUP BY `card_id`
) AS `trans_max`
FROM `trans`
LEFT JOIN `merchants` ON `merchants`.`merchantkey`=`trans`.`merchantkey`
WHERE trans.card_id = users.card_id
AND trans.card_id = trans_max.card_id
AND trans.trans_id = trans_max.trans_dt
ORDER BY users.lastname, users.firstname;
答案 1 :(得分:0)
错误表明您的merchantkey
表中实际上没有trans
字段。我认为你确实有这个领域,错误是由混合连接样式引起的。您已经弃用了ANSI-89样式(FROM tablea, tableb WHERE a.key = b.key
)以及正确的ANSI-92样式(FROM tablea JOIN tableb ON a.key = b.key
)。两者都独立工作,但混合它们会产生优先级问题,因为在ANSI-89样式连接之前评估ANSI-92样式连接。此优先级问题会导致错误,因为您引用了LEFT JOIN
中尚未评估的字段。切换到所有正确的ANSI-92样式连接是很好的做法,并且应该修复您的错误:
SELECT DISTINCT (users.card_id ) AS card_id2
, users .firstname AS `First Name`
, users.lastname AS `Last Name`
, users.card_id AS `Card ID`
, users.address1 AS Address
, users.address2 AS Address2
, users.city AS City
, users.state AS State
, users.zip AS Zip
, users.email AS Email
, users.phone_area
, users.phone_exchange
, users.phone_number
, trans.trans_dt AS `Last Trans Date`
, trans.credit_balance AS `Credit Balance`
, trans.balance AS `Gift Balance`
, users.gender AS Gender
, users.birthdate AS `Birthday (Age)`
, users.register_dt AS `Registration Date`
, merchants.company
, merchants.merchantkey
FROM trans
JOIN users
ON trans.card_id = users.card_id
JOIN (SELECT card_id
, max( trans_id ) AS trans_dt
FROM trans
WHERE trans.trans_type > 0
GROUP BY card_id
) AS trans_max
ON trans.card_id = trans_max.card_id
AND trans.trans_id = trans_max.trans_dt
LEFT JOIN merchants
ON merchants.merchantkey = trans.merchantkey
ORDER BY users.lastname, users.firstname;
MySql文档:http://dev.mysql.com/doc/refman/5.7/en/join.html
以前,逗号运算符(,)和JOIN都有相同的含义 优先级,因此连接表达式t1,t2 JOIN t3被解释为 ((t1,t2)JOIN t3)。现在JOIN具有更高的优先级,所以表达式 被解释为(t1,(t2 JOIN t3))。此更改会影响语句 使用ON子句,因为该子句只能引用列 在连接的操作数中,优先级的变化会发生变化 解释这些操作数是什么。
看起来这个优先级问题并不总是存在,但它可能已经在MySQL 5.7中引入了,所以如果查询以前有效,那么它可能是以前在早期版本上运行的。