[英]mysql select data from result of alias name on LEFT JOIN condition
SELECT e.`event_type`,e.`event_status`,er.`event_reg_id`,er.`event_id`,'events' as category,
IF(e.`event_type`='C',e.`parent_id` ,IF(e.`event_type`='S',er.`event_id`,'') ) temp_id
FROM `event_registration` er
LEFT JOIN event e on er.`event_id`= temp_id WHERE er.`company_id`='%s' AND er.`student_id`='%S'
I take temp_id
as alias name and give it to LEFT JOIN ON condition, it not works. 我将temp_id
作为别名,并将其设置为LEFT JOIN ON条件,它不起作用。 I gave alias name instead of column name on LEFT JOIN ON Condition 我在LEFT JOIN ON条件下给了别名而不是列名
You can not use the temp_id
alias on the ON
part of your query. 您不能在查询的ON
部分使用temp_id
别名。 The alias is not available there. 别名在那里不可用。 But you can do the following moving the nested IF
to the ON
part: 但是您可以执行以下操作,将嵌套的IF
移到ON
部分:
SELECT
e.`event_type`,
e.`event_status`,
er.`event_reg_id`,
er.`event_id`,
'events' AS category,
IF(e.`event_type` = 'C', e.`parent_id`, IF(e.`event_type` = 'S', er.`event_id`,'')) AS temp_id,
FROM `event_registration` er
LEFT JOIN event e ON er.`event_id`= IF(e.`event_type` = 'C', e.`parent_id`, IF(e.`event_type` = 'S', er.`event_id`,''))
WHERE er.`company_id` = '%s' AND er.`student_id` = '%S'
You can't use an alias from SELECT
because the FROM
part is executed earlier ( see this answer on StackOverflow ). 您不能使用SELECT
的别名,因为FROM
部分是较早执行的( 请参见StackOverflow上的此答案 )。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.