我的模型看起来像这样:
author: id, name, lastname
books: id, name, author_id
当我加入请求时:
SELECT author.*, books.*
FROM author
LEFT JOIN books ON books.author_id = author.id
我的结果集中列名称引用的内容尚不清楚。
我可以通过以下方式解决这个问题:
SELECT author.*, books.*, author.name as `author_name`, books.name as `book_name`
...
但我不想明确每个字段别名。我想做的是这样的事情:
SELECT author.* as `author_.*`, books.* as `books_.*`
...
所以我有列author_id, author_name, ..., book_name, ...
但它不起作用。你怎么避免名字混乱?我是否必须为每个字段指定别名?
答案 0 :(得分:4)
如果您不希望在列上添加ALIAS
,则问题的最佳解决方案是 将其重命名以避免名称冲突 。< / p>
答案 1 :(得分:0)
输出将仅显示字段名称(或查询中指定的别名),而不是表名称。将模型更改为包含&#39; author_name&#39;,&#39; book_name&#39;等字段,或在查询中使用别名。