代码之家  ›  专栏  ›  技术社区  ›  Lukas Oppermann

SQL选择table1.columa作为table1.columb

  •  2
  • Lukas Oppermann  · 技术社区  · 16 年前

    我正在处理数据库联接,我想执行以下操作:

    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 回复  |  直到 16 年前
        1
  •  3
  •   Community Mohan Dere    9 年前

    一般来说,您只需使用:

    table_two.value as key
    

    否则,作为 OMG Ponies suggested in another answer ,可以将别名包装为反记号:

    table_two.value as `table_two.key`
    
        2
  •  1
  •   OMG Ponies    16 年前

    这是:

    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
    

        3
  •  0
  •   Community Mohan Dere    9 年前

    从你的 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'
    
    推荐文章