2016-10-04 44 views
-1

结合我有一个表牙科 列是(标题,出版社,关键词,主题)和另一 表,我有爱思唯尔 列是(作者,标题,期刊,出版社,抽象,关键字 和第三表圣人 列是(作者,标题,期刊,出版商,摘要,关键字)。 问题是我怎样才能将这些表通过union组合起来,以便我可以从所有三个表中的数据库中获取数据。 我不能使用只能加入我有工会所以建议如果,如果有对此联盟与多台不同的列如何

当我使用任何解决方案选项:

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

看答案就[ “MySQL的工会不同的列数”(HTTP://计算器的.com /问题/ 15867152/RQ的MySQL工会-不同-数的柱α= 1)。我建议他们的方法(“空”值不重叠列) – Insac

+0

没有得到适当的参考值cos这两个表有不同的列数 –

+0

但是,这种方法可以推广..看看我的建议答案 – Insac

回答

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%' 
) 
+0

#1054 - 在'where子句'中未知列'作者'在mysql中获取此错误.. –

+0

你能否粘贴你正在测试的代码?也许你在强迫混合大小写的名字? – Insac

+0

我可以发表你的查询吗,我通过你的结构 –

0

第二个表格和第三个表格使用别名作为第一个表格字段名称

+0

可以用户界面请发送给我的结构? –

+0

(选择null作为作者,标题,null作为期刊,发布者,null作为发布者,null作为抽象,关键字,主题来自牙科,其中1)union(选择作者,标题作为标题,期刊,出版商,摘要,关键字,来自elsevier的关键字其中1)union(选择null作为作者,标题,null作为日记,发布者,null作为摘要,关键字,来自sage的主题,其中1) –

+0

#1054 - 未知列'作者'在'where子句'中获取此错误 –