SQL选择table1.column作为表1.column

时间:2010-04-08 19:52:15

标签: sql mysql select join

我正在进行数据库连接,我想执行以下操作:

Select tabel_one.id, 
       tabel_one.title, 
       tabel_one.content, 
       table_two.value as table_two.key 
  from tabel_one 
  Join table_two ON table_two.id = table_one.id ....

重要的部分是:

table_two.value as table_two.key

有没有办法可行?

3 个答案:

答案 0 :(得分:3)

不,您无法定义引用表的别名。

一般情况下,您应该使用:

table_two.value as key

否则,作为OMG Ponies suggested in another answer,您可以在反引号中包装别名:

table_two.value as `table_two.key`

答案 1 :(得分:1)

此:

Select tabel_one.id, 
       tabel_one.title, 
       tabel_one.content, 
       table_two.value as `table_two.key` 
  from tabel_one 
  Join table_two ON table_two.id = table_one.id

...适用于MySQL 5.1.35。由于这段时间,你需要用反引号括起别名

答案 2 :(得分:0)

根据您的comment on this answer判断,我希望您真正想要的是:

Select tabel_one.id,  
       tabel_one.title,  
       tabel_one.content,  
       table_two.value as tags,
       table_three.value as author
  from table_one  
  LEFT Join table_two ON table_two.id = table_one.id 
  AND table_two.key= 'tags'
  LEFT Join table_three ON table_three.id = table_one.id 
  AND table_three.key= 'author'