sql - mysql JOIN ON IF()? -
i trying use sql such this:
select t.*, t2.* templates t left join if(t.t_type = 0,'templates_email', if(t.t_type = 1,'templates_sms','templates_fax')) t2 on t.t_id = t2.t_id;
is possible that?
basically want join on 1 of 3 tables based on value row.
is recommended if possible?
update
so,
basically templates
table table contains information every template must have, eg name, id, description
then have templates_x
tables these tables contain fields unique each template type.
(there quite few , having single table null fields not applicable not practical).
the tables called templates_x
appropriate x stored in templates table int flag.
the join between templates_x
tables , templates
table through t_id
.
so suggest?
is possible that?
no, can't use dynamically assigned tables (join or otherwise) without using dynamic sql syntax (see mysql's preparedstatement syntax).
this non-dynamic re-write of pseudo-query assumes 3 template tables join have same number of columns, , same data types:
select t.*, te.* templates t join templates_email te on te.t_id = t.t_id t.t_type = 0 union select t.*, ts.* templates t join templates_sms ts on ts.t_id = t.t_id t.t_type = 1 union select t.*, tf.* templates t join templates_fax tf on tf.t_id = t.t_id t.t_type not in (0, 1)
Comments
Post a Comment