联合多表与不同列如何结合

时间:2016-10-04 07:00:38

标签: php mysql

我有桌牙     列是(标题,出版商,关键字,主题)和另一个    我还有其他的表    列是(作者,标题,期刊,出版商,摘要,关键字)    和第三桌圣人    列是(作者,标题,期刊,出版商,摘要,关键字)。     问题是如何通过union all将这些表组合起来,这样我就可以从所有三个表中获取数据库中的数据。     我不能使用加入选项我有工会所以建议如果有任何解决方案

当我使用时:

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'

2 个答案:

答案 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%'
)