I’m doing UNION in MySQL that I’m unable to troubleshoot for a while. Error says that
syntax is incorrect around
t1.*
Those 2 SELECTs work ok separately, checked. But UNION fails. I’m not custom to MySQL syntax, maybe something is wrong with that.
SELECT ( t1.*, a.region_count FROM ( SELECT data_region, COUNT(*) AS region_count FROM t2 GROUP BY data_region ) AS a LEFT OUTER JOIN t1 ON t1.values_att0 = a.data_region WHERE t1.name_0 = 'region' ) AS b UNION SELECT ( t1.*, c.age_gen_count FROM ( SELECT data_dage, data_gen, COUNT(*) AS age_gen_count FROM t2 GROUP BY data_dage, data_gen ) AS c LEFT JOIN t1 ON t1.values_att0 = c.data_dage AND t1.id_question_1 = c.data_gen WHERE t1.name_0 = 'age' AND t1.q_name_1 = 'gen' )
Advertisement
Answer
You are using parenthesis around your SELECT
field, this is your syntax error origin (the UNION
is not the cause). Just remove them:
SELECT t1.*, a.region_count FROM ( SELECT data_region, COUNT(*) AS region_count FROM t2 GROUP BY data_region ) AS a LEFT OUTER JOIN t1 ON t1.values_att0 = a.data_region WHERE t1.name_0 = 'region' UNION ALL SELECT t1.*, c.age_gen_count FROM ( SELECT data_dage, data_gen, COUNT(*) AS age_gen_count FROM t2 GROUP BY data_dage, data_gen ) AS c LEFT JOIN t1 ON t1.values_att0 = c.data_dage AND t1.id_question_1 = c.data_gen WHERE t1.name_0 = 'age' AND t1.q_name_1 = 'gen'