dpwu16132 2015-03-17 16:37
浏览 36
已采纳

INNER JOIN独有的列mysql

I have planned to use three tables for my sites tagging system, they looks like:

|-----------------------|
|==> photo              |
|   -> id               |
|   -> custom_id        |
|   -> title            |
|   -> slug             |
|   -> date             |
|   -> image_url        |
|-----------------------|
|==> tags               |
|   -> id               |
|   -> slug             |
|   -> title            |
|-----------------------|
|==> tags_relation      |
|   -> tid              | <-- this is the tags.id
|   -> pid              | <-- this is the photo.custom_id
|-----------------------|

So, to fetch the recent posts of a specific tag i am using INNER JOIN by the following query:

        SELECT p.id, p.custom_id, p.title, p.slug, p.date, p.image_url, t.id, t.slug, tr.* 
        FROM tags t INNER JOIN
            tags_relation tr
            ON t.id = tr.tid INNER JOIN
            photo p
            ON p.custom_id = tr.pid
        WHERE t.slug = 'people' 
        ORDER BY p.date DESC 
        LIMIT 20 

Everything works fine except the fact that the 'slug, id, title' column of the photo table is being replaced by the 'slug, id, title' column of the tags table!
I figured out a solution that is changing the tags columns name, but is there any best practices to solve this problem? Thanks

  • 写回答

3条回答 默认 最新

  • dongzhang8475 2015-03-17 16:44
    关注

    I think that you should use Aliases. For example:

    SELECT p.id AS Person_Id, p.title AS Person_Title ...
    

    You can learn more about aliases here.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择
  • ¥15 部分网页页面无法显示!
  • ¥15 怎样解决power bi 中设置管理聚合,详细信息表和详细信息列显示灰色,而不能选择相应的内容呢?
  • ¥15 QTOF MSE数据分析
  • ¥15 平板录音机录音问题解决