当我使用时:
select null author, Title, null journal, publisher, subject as abstract, keyword from table1
union
select author, title, journal, publisher, abstract,keyword from table2
select null author,Title,null journal,publisher,null abstract,subject as abstract,keyword from dental
where (Author LIKE '%Bastone%' or Title LIKE '%Bastone%' or Journal LIKE '%Bastone%'
or Publisher LIKE '%Bastone%' or abstracts LIKE '%Bastone%' or Keyword LIKE '%Bastone%'
or Title LIKE '%Bastone%' or Publisher LIKE '%Bastone%' or Keyword LIKE '%Bastone%'
or Subject LIKE '%Bastone%')
union
select author, title, journal, publisher, abstract,keyword, null subject from elsevier
where(Author LIKE '%Bastone%' or Title LIKE '%Bastone%' or Journal LIKE '%Bastone%'
or Publisher LIKE '%Bastone%' or abstracts LIKE '%Bastone%' or Keyword LIKE '%Bastone%'
or Title LIKE '%Bastone%' or Publisher LIKE '%Bastone%' or Keyword LIKE '%Bastone%'
or Subject LIKE '%Bastone%')
收到此错误
#1054 - Unknown column 'Author' in 'where clause'
答案 0 :(得分:0)
将第二个表和第三个表的别名用作第一个表字段名称
答案 1 :(得分:0)
可行的方法可能是:
select null author, Title, null journal, publisher, null abstract, keyword, subject
from table1
union all
select author, title, journal, publisher, abstract,keyword, null subject
from table2
union all
select author, title, journal, publisher, abstract,keyword, null subject
from table3
如果其中一些字段只是“同义词”(例如抽象和主题),您可以通过对它们进行别名(例如“抽象为主题”)使它们在同一列中返回。见下面的例子:
select null author, Title, null journal, publisher, subject as abstract, keyword
from table1
union all
select author, title, journal, publisher, abstract,keyword
from table2
union all
select author, title, journal, publisher, abstract,keyword
from table3
在澄清查询结构后
如果您需要将where条件应用于结果表,则最好将它们包装到查询中。
示例:
select * from (
select null author, Title, null journal, publisher, subject as abstract, keyword
from table1
union all
select author, title, journal, publisher, abstract,keyword
from table2
union all
select author, title, journal, publisher, abstract,keyword
from table3
) as table_union
where author like '%string%'
如果出于某种原因,您无法包装整个查询(即查询框架的限制),则可以包装单个查询。
因此,您的原始查询可以写成:
select * from (
select null author, Title, null journal, publisher, null abstract, keyword, subject
from dental
union all
select author, title, journal, publisher, abstract,keyword, null subject
from elsevier
) as table_union
where (Author LIKE '%Bastone%'
or Title LIKE '%Bastone%'
or Journal LIKE '%Bastone%'
or Publisher LIKE '%Bastone%'
or abstract LIKE '%Bastone%'
or Keyword LIKE '%Bastone%'
or Subject LIKE '%Bastone%'
)
或:
select * from (
select null author, Title, null journal, publisher, null abstract, keyword, subject
from dental
) as dental_2
where (Author LIKE '%Bastone%'
or Title LIKE '%Bastone%'
or Journal LIKE '%Bastone%'
or Publisher LIKE '%Bastone%'
or abstract LIKE '%Bastone%'
or Keyword LIKE '%Bastone%'
or Subject LIKE '%Bastone%'
)
union all
select * from (
select author, title, journal, publisher, abstract,keyword, null subject
from elsevier
) as elsevier_2
where (Author LIKE '%Bastone%'
or Title LIKE '%Bastone%'
or Journal LIKE '%Bastone%'
or Publisher LIKE '%Bastone%'
or abstract LIKE '%Bastone%'
or Keyword LIKE '%Bastone%'
or Subject LIKE '%Bastone%'
)