dongleiqiao2107 2014-05-07 17:54
浏览 14
已采纳

如何使用链接表选择名称?

So I have a check to see if you're allowed to view that folder(currently checking if the ip is my home ip)

I want this check updated into checking if your account has the rights. So i want to get all the names from gallery_folders where user_id = :user_id (yes pdo, but that doesn't matter for this)

SELECT      f.name 
FROM        gallery_folders     as f 
LEFT JOIN   gallery_link        as p 
WHERE       f.id = p.folder_id
AND         p.user_id = :user_id

This is where I got stuck, I'm not sure how to add user_id to this so it'll only get all the (p.folder_id where user_id=:user_id)

Databse structure if necessary:

Folders

  • gallery_folders
    • id
    • name

Users

  • gallery_users
    • id
    • username
    • password
    • token

Link table

  • gallery_link
    • id
    • user_id
    • gallery
  • 写回答

1条回答 默认 最新

  • douzi2778 2014-05-07 18:08
    关注

    If you only want the folders where the user has the proper rights then you will want to change the join from LEFT JOIN to INNER JOIN. You also need to fix your join statement so that it is ON f.id = p.folder_id instead of WHERE f.id = p.folder_id.

    I think this is what you are after:

    SELECT f.name 
    FROM gallery_folders f 
    INNER JOIN gallery_link p 
    ON f.id = p.folder_id
    WHERE p.user_id = :user_id
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算