具有不同列数的UNION多选语句

时间:2013-07-28 00:21:43

标签: mysql select union union-all

如何结合每个表具有不同列数的这些select语句?

(SELECT title, description, full_description, keywords, option1, option2, option3, option4, option5, option6, option7, option8 FROM articles WHERE title LIKE ? OR description LIKE ? OR full_description LIKE ? OR keywords LIKE ? OR option1 LIKE ? OR option2 LIKE ? OR option3 LIKE ? OR option4 LIKE ? OR option5 LIKE ? OR option6 LIKE ? OR option7 LIKE ? OR option8 LIKE ?) 
UNION ALL 
(SELECT title, description, full_description, keywords, option1, option2, option3, option4, option5, option6, option7, option8 FROM news WHERE title LIKE ? OR description LIKE ? OR full_description LIKE ? OR keywords LIKE ? OR option1 LIKE ? OR option2 LIKE ? OR option3 LIKE ? OR option4 LIKE ? OR option5 LIKE ? OR option6 LIKE ? OR option7 LIKE ? OR option8 LIKE ?) 
UNION ALL 
(SELECT title, description, full_description, keywords, option1, option2, option3, option4, option5, option6, option7, option8 FROM products WHERE title LIKE ? OR description LIKE ? OR full_description LIKE ? OR keywords LIKE ? OR option1 LIKE ? OR option2 LIKE ? OR option3 LIKE ? OR option4 LIKE ? OR option5 LIKE ? OR option6 LIKE ? OR option7 LIKE ? OR option8 LIKE ?) 
UNION ALL 
(SELECT title, description, option1, option2, option3, option4, option5, option6, option7, option8 FROM gallery_images WHERE title LIKE ? OR description LIKE ? OR option1 LIKE ? OR option2 LIKE ? OR option3 LIKE ? OR option4 LIKE ? OR option5 LIKE ? OR option6 LIKE ? OR option7 LIKE ? OR option8 LIKE ?) 
UNION ALL 
(SELECT title, description, option1, option2, option3, option4, option5, option6, option7, option8 FROM pages WHERE title LIKE ? OR description LIKE ? OR option1 LIKE ? OR option2 LIKE ? OR option3 LIKE ? OR option4 LIKE ? OR option5 LIKE ? OR option6 LIKE ? OR option7 LIKE ? OR option8 LIKE ?)

我收到了这个错误:

The used SELECT statements have a different number of columns

2 个答案:

答案 0 :(得分:1)

简单地说:您不能 - 您必须向那些列数少于列数的子查询添加一些空白列(例如NULL as blank1

答案 1 :(得分:1)

只需使用NULL值作为缺少字段的占位符。

这应该有效:

(SELECT title, description, full_description, keywords, option1, option2, option3, option4, option5, option6, option7, option8 FROM articles WHERE title LIKE ? OR description LIKE ? OR full_description LIKE ? OR keywords LIKE ? OR option1 LIKE ? OR option2 LIKE ? OR option3 LIKE ? OR option4 LIKE ? OR option5 LIKE ? OR option6 LIKE ? OR option7 LIKE ? OR option8 LIKE ?) 
UNION ALL 
(SELECT title, description, full_description, keywords, option1, option2, option3, option4, option5, option6, option7, option8 FROM news WHERE title LIKE ? OR description LIKE ? OR full_description LIKE ? OR keywords LIKE ? OR option1 LIKE ? OR option2 LIKE ? OR option3 LIKE ? OR option4 LIKE ? OR option5 LIKE ? OR option6 LIKE ? OR option7 LIKE ? OR option8 LIKE ?) 
UNION ALL 
(SELECT title, description, full_description, keywords, option1, option2, option3, option4, option5, option6, option7, option8 FROM products WHERE title LIKE ? OR description LIKE ? OR full_description LIKE ? OR keywords LIKE ? OR option1 LIKE ? OR option2 LIKE ? OR option3 LIKE ? OR option4 LIKE ? OR option5 LIKE ? OR option6 LIKE ? OR option7 LIKE ? OR option8 LIKE ?) 
UNION ALL 
(SELECT title, description, NULL AS full_description, NULL AS keywords, option1, option2, option3, option4, option5, option6, option7, option8 FROM gallery_images WHERE title LIKE ? OR description LIKE ? OR option1 LIKE ? OR option2 LIKE ? OR option3 LIKE ? OR option4 LIKE ? OR option5 LIKE ? OR option6 LIKE ? OR option7 LIKE ? OR option8 LIKE ?) 
UNION ALL 
(SELECT title, description, NULL AS full_description, NULL AS keywords, option1, option2, option3, option4, option5, option6, option7, option8 FROM pages WHERE title LIKE ? OR description LIKE ? OR option1 LIKE ? OR option2 LIKE ? OR option3 LIKE ? OR option4 LIKE ? OR option5 LIKE ? OR option6 LIKE ? OR option7 LIKE ? OR option8 LIKE ?)