我目前在多个 进入案例时遇到问题。当我删除第二个 时,它可以正常工作。这有什么问题?
报告的MYSQL错误是:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'as param2 on param2.param_micro_site=i1.i_micro_site else null END )' at line 1
我尝试了这些建议:
MySQL - Combining multiple WHEN conditions in CASE
How to write a MYSQL CASE WHEN statement with multiple search conditions?
但它并没有解决我的问题。
SELECT * FROM items i1
join param on
(
case
when (ITEM_ID=param_item_id and i_status=1 and item_page=164)
then param_item_id=ITEM_ID
when (i_micro_site>=1 and i_status=7 and (EXISTS(select * from multiple where multiple_id=ITEM_ID and multiple_cat=21 and multiple_enum="item") || item_page=169))
then (SELECT * from items i2 join pages on i2.item_page=p_ID and p_cat in (21,29,0) join param where param_micro_site=i1.i_micro_site and param_item_id in(i1.ITEM_ID,i2.ITEM_ID)) as param2 on param2.param_micro_site=i1.i_micro_site
else null END
)
谢谢
答案 0 :(得分:1)
下面是尝试以一种可读的方式格式化您的查询:
SELECT *
FROM items i1 join
param
on (case when (ITEM_ID=param_item_id and i_status=1 and item_page=164)
then param_item_id=ITEM_ID
when (i_micro_site>=1 and i_status=7 and
(EXISTS(select *
from multiple
where multiple_id=ITEM_ID and multiple_cat=21 and multiple_enum="item"
) || item_page=169
)
)
then (SELECT *
from items i2 join
pages on i2.item_page=p_ID and p_cat in (21,29,0) join
param
where param_micro_site=i1.i_micro_site and
param_item_id in(i1.ITEM_ID,i2.ITEM_ID)
) as param2
on param2.param_micro_site=i1.i_micro_site
else null END
)
以下是on
之后param2
的观察结果:
then
子句的子查询已完成。then
子句中的子查询不能使用列(或表)别名。*
也会带回多个列。除了语法之外,这个查询完全不可读。我建议您提出另一个问题,提供表格布局,示例数据和所需结果。有人可能想出一种更好的方法来编写您尝试过的查询。