SQL Select table1.columa as table1.columb
I am working on a database join and I want to do the following:
Select tabel_one.id,
tabel_one.title,
tabel_one.content,
table_two.value as table_two.ke开发者_如何学Cy
from tabel_one
Join table_two ON table_two.id = table_one.id ....
The Important part is:
table_two.value as table_two.key
Is there a way this could work?
No, you cannot define an alias referencing a table.
In general, you should simply use:
table_two.value as key
Otherwise, as OMG Ponies suggested in another answer, you can wrap the alias in backticks:
table_two.value as `table_two.key`
This:
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
...works for me on MySQL 5.1.35. Because of the period, you need to enclose the alias with backticks
Judging by your comment on this answer, I expect what you really want is:
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'
精彩评论