0

how can i union these select statements that each table has different number of columns?

(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 ?)

i recieve this error :

The used SELECT statements have a different number of columns
4

2 回答 2

1

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

于 2013-07-28T00:23:24.187 回答
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 ?)
于 2013-07-28T00:25:42.787 回答