我在下面的查询返回以下错误消息“'on clause'中的未知列'O.order_id'”。我猜这是一个优先问题?但我没有足够的背景来弄清楚可能的位置。
我刚刚添加了'where'子句,以便我可以过滤产品类别。
SELECT
MAX(
CASE WHEN OIM.meta_key = 'enrolment_id' THEN(
SELECT
firstname
FROM
wp_sp_enrolments E
WHERE
OIM.meta_value = E.id
)
END
) AS 'firstname',
MAX(
CASE WHEN OIM.meta_key = 'enrolment_id' THEN(
SELECT
surname
FROM
wp_sp_enrolments E
WHERE
OIM.meta_value = E.id
)
END
) AS 'surname',
O.order_item_name AS 'school',
MAX(
CASE WHEN OIM.meta_key = 'class' THEN OIM.meta_value
END
) AS 'class',
MAX(
CASE WHEN OIM.meta_key = 'fee-type' THEN OIM.meta_value
END
) AS 'fees',
MAX(
CASE WHEN OIM.meta_key = 'enrolment_Id' THEN(
SELECT
UM.meta_value
FROM
wp_usermeta UM,
wp_sp_enrolments E
WHERE
UM.meta_key = '_user_phone' AND UM.user_id = E.memberid AND E.id = OIM.meta_value
LIMIT 1
)
END
) AS 'phone', MAX(
CASE WHEN OIM.meta_key = 'enrolment_id' THEN(
SELECT
U.user_email
FROM
wp_users U,
wp_sp_enrolments E
WHERE
U.id = E.memberid AND E.id = OIM.meta_value
LIMIT 1
)
END
) AS 'email', P.post_status AS 'status'
FROM
wp_woocommerce_order_items O,
wp_terms T,
wp_term_taxonomy TT,
wp_term_relationships TR,
wp_woocommerce_order_itemmeta OIM
INNER JOIN wp_posts P ON
P.ID = O.order_id
WHERE
T.name = 'workshops' AND T.term_id = TT.term_id AND TT.taxonomy = 'product_cat' AND TT.term_id = TR.term_taxonomy_id AND TR.object_id = OIM.meta_value AND OIM.meta_key = '_product_id' AND OIM.order_item_id = O.order_item_id
GROUP BY
O.order_item_id
问题是,如您所怀疑的,逗号运算符和连接运算符的优先级。
在https://dev.mysql.com/doc/refman/8.0/en/join.html上声明:
但是,逗号运算符的优先级小于INNER JOIN,CROSS JOIN,LEFT JOIN等的优先级。如果在存在连接条件时将逗号连接与其他连接类型混合,则可能会出现'on子句'中未知列'col_name'形式的错误。
意思是,你不应该混合,最好。
所以不是内联,而是
, wp_posts P
在WHERE
and P.id=O.order_id and P.id is not null
或者,您可以将逗号分隔的表捆绑在括号中:
(wp_woocommerce_order_items O,
wp_terms T,
wp_term_taxonomy TT,
wp_term_relationships TR,
wp_woocommerce_order_itemmeta OIM )
INNER JOIN ...
另一个例子,为什么我对逗号运算符有如此强烈的厌恶。
不确定,但尝试以下做INNER JOIN:
SELECT
MAX(
CASE WHEN OIM.meta_key = 'enrolment_id' THEN(
SELECT
firstname
FROM
wp_sp_enrolments E
WHERE
OIM.meta_value = E.id
)
END
) AS 'firstname',
MAX(
CASE WHEN OIM.meta_key = 'enrolment_id' THEN(
SELECT
surname
FROM
wp_sp_enrolments E
WHERE
OIM.meta_value = E.id
)
END
) AS 'surname',
O.order_item_name AS 'school',
MAX(
CASE WHEN OIM.meta_key = 'class' THEN OIM.meta_value
END
) AS 'class',
MAX(
CASE WHEN OIM.meta_key = 'fee-type' THEN OIM.meta_value
END
) AS 'fees',
MAX(
CASE WHEN OIM.meta_key = 'enrolment_Id' THEN(
SELECT
UM.meta_value
FROM
wp_usermeta UM,
wp_sp_enrolments E
WHERE
UM.meta_key = '_user_phone' AND UM.user_id = E.memberid AND E.id = OIM.meta_value
LIMIT 1
)
END
) AS 'phone', MAX(
CASE WHEN OIM.meta_key = 'enrolment_id' THEN(
SELECT
U.user_email
FROM
wp_users U,
wp_sp_enrolments E
WHERE
U.id = E.memberid AND E.id = OIM.meta_value
LIMIT 1
)
END
) AS 'email', P.post_status AS 'status'
FROM wp_woocommerce_order_items O
INNER JOIN wp_woocommerce_order_itemmeta OIM ON
O.order_item_id = OIM.order_item_id
INNER JOIN wp_term_relationships TR ON
O.order_id = OIM.meta_value
INNER JOIN wp_term_taxonomy TT ON
TR.term_taxonomy_id = TT.term_taxonomy_id
INNER JOIN wp_terms T ON
T.term_id = TT.term_id
INNER JOIN wp_posts P ON
P.ID = O.order_id
WHERE T.name = 'workshops'
AND TT.taxonomy = 'product_cat'
AND OIM.meta_key = '_product_id'
GROUP BY
O.order_item_id