dongyaobo9081 2015-11-09 10:43
浏览 19
已采纳

PHP / MySQL关系/回声场

I'm trying to build my 1st relationship database and it looks like this so far:

Relationships Table

Relationship Table

Partners Table

Partners

Location Table

Locations

I want to echo all the row info including the related location names on my page, how would I get the following to echo?

ID: 2 Name: Salisbury Removals Locations: Salisbury
ID: 4 Name: Inbetween Removals Locations: Salisbury, Southampton
ID: 5 Name: Southampton Removals Locations: Southampton

=====SOLVED!=====

$sql = "SELECT partner_id, partner_name, email_address, active FROM partners WHERE active ='yes' ORDER BY partner_id ASC";


                                $connect->query($sql);

                                if ($partners = $connect->query($sql)) {

                                    foreach ($partners as $partner) {

                                        echo '<li><ul>';
                                        echo '<li>' . $partner['partner_id'] . '</li>';
                                        echo '<li>' . $partner['partner_name'] . '</li>';
                                        echo '<li>' . $partner['email_address'] . '</li>';
                                        echo '<li>' . $partner['active'] . '</li>';

                                        // START GET LOCATIONS FROM RELATED TABLE
                                        echo '<ul>';

                                        $sql2 = "SELECT p.partner_name AS Name, p.partner_id AS ID, l.location_name AS Locations from partners_locations r, partners p, locations l WHERE p.partner_id = r.partner_id AND l.location_id = r.location_id AND r.partner_id =" . $partner['partner_id'] . "";

                                        $connect->query($sql2);

                                        if ($locations = $connect->query($sql2)) {

                                            foreach ($locations as $location) {

                                                echo '<li>' . $location['Locations'] . '</li>';
                                            }

                                        } else {

                                            echo "Error: No Locations<br>";

                                        }

                                        echo '</ul>';
                                        // END GET LOCATIONS FROM RELATED TABLE

                                        echo '</ul></li>';

                                    }

                                } else {

                                     echo "Error: No Active Partners<br>";

                                }
  • 写回答

4条回答 默认 最新

  • dongzhuang6177 2015-11-09 11:08
    关注

    The best thing you could do is make a new table to contain the partner_id and location_id.

    tbl_relationships_new

    enter image description here

    Pros for this approach :-

    1).When you need to remove a location from a partner, you wouldn't need to edit the column locations. You could simply delete an entry from this new table.

    2). When you need to add more data in the locations field, you could simply just insert into the new table, which is rather easy than having to update partners.locations.

    Now, you could use easy left joins to get the required data. SQL query for my table solution.

    SELECT t.*,p.*,l.* FROM tbl_relationships_new t, partners p, locations l LEFT JOIN 
        partners 
        ON
        t.partner_id = p.partner_id 
        LEFT JOIN 
        locations 
        ON 
        l.location_id = t.location_id 
        WHERE
        t.partner_id = 2
    

    UPDATE

    Here are the queries based on your table structure.

    1). ID: 2 Name: Salisbury Removals Locations: Salisbury

    SELECT p.partner_name AS Name, p.partner_id AS ID, l.location_name AS Locations from relationships r, partners p, locations l WHERE p.partner_id = r.partner_id AND l.location_id = r.location_id AND r.partner_id = 2
    

    enter image description here

    2). When there are 2 locations.

    SELECT p.partner_name AS Name, p.partner_id AS ID, l.location_name AS Locations from relationships r, partners p, locations l WHERE p.partner_id = r.partner_id AND l.location_id = r.location_id AND r.partner_id = 5
    

    enter image description here

    UPDATE

    Solution without explicitly mentioning an ID.

    SELECT p.partner_name AS Name, p.partner_id AS ID, l.location_name AS Locations from relationships r, partners p, locations l WHERE p.partner_id = r.partner_id AND l.location_id = r.location_id
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果