简体   繁体   English

PHP和MySQL,使用外键从其他表中获取数据

[英]PHP and MySQL, Using a foreign key to get data from another table where it originates from

This question involves 3 tables that I have in PHPmyAdmin called: 这个问题涉及到我在PHPmyAdmin中拥有的3个表:

  • tblShoots
  • tblMemberOnShoot
  • tblMember

I need to run a query on a users dashboard which tells them, which photoshoots they have been to.The tblmemberOnShoot table is shown below, if I was to click on through an entry under fldShootID (which is a foreign key), it would take me to tblShoots which holds the shoot details, in there I need to pull out fldShootLocation . 我需要在用户仪表板上运行查询,告诉他们去过哪些照片tblmemberOnShoot表如下所示,如果我要单击fldShootID (这是一个外键)下的条目,则需要我进入保存拍摄细节的tblShoots ,在那里我需要拉出fldShootLocation

tblMemberOnShoot表

At the moment my query is shown below which doesn't entirely give the output needed, I need help with the join? 在下面显示我的查询时,它不能完全提供所需的输出,我需要联接方面的帮助吗?

<?php 
        $query = "SELECT * FROM `tblMembersOnShoot` WHERE `fldMemberID` = 1";

        $result = $conn -> query($query);

        while($row = $result -> fetch_assoc()) 
            {
                echo $row['fldShootID']."<br>";

            } 
    ?>

Output on the page: 页面上的输出: 在此处输入图片说明

As you said, a JOIN is the way to go. 如您所说, JOIN是必经之路。 Here's what I'd suggest: 这是我的建议:

SELECT fldShootLocation FROM tblMember
LEFT JOIN tblMembersOnShoot
ON tblMembersOnShoot.fldMemberID = tblMember.fldMemberID
LEFT JOIN tblShoots
ON tblShoots.fldShootID = tblMembersOnShoot.fldShootID
WHERE tblMember.fldMemberID = 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM