简体   繁体   中英

PHP Mysql Join issue

I am selecting data from 3 tables using a two left joins.

This all works up until the iteration of one of the values for an association (i think, through process of elimination).

I now have reason to believe the select statement is incorrect at the point of the JOINs, but i cannot see how.

The join that is not functioning as expected:

LEFT JOIN
         ae_template_pageTypes t ON t.ae_template_pageTypes_id = tp.ae_template_page_group_id

Here i am trying to get the ae_template_pageTypes_type_label by the ae_template_pageTypes_id association.

Table: ae_template_pages ae_template_pages

Table: ae_template_pageTypes ae_template_pageTypes

Table: ae_template_groups

ae_template_groups

The HTML Output (Both showing HTML instead of using join respectively) HTML输出

THE ISSUE I am expecting 2 different values in the format section where here shows both as HTML. I cannot locate why this is happening.

The SELECT

       SELECT tg.ae_template_group_name, tp.ae_template_page_id, tp.ae_template_page_group_id, tp.ae_template_page_title, tp.ae_template_page_type, tp.is_group_index, t.ae_template_pageTypes_id, t.ae_template_pageTypes_type_label
        FROM
          ae_template_pages tp
        LEFT JOIN
          ae_template_pageTypes t ON t.ae_template_pageTypes_id = tp.ae_template_page_group_id
        LEFT JOIN
          ae_template_groups tg ON tg.ae_template_group_id = tp.ae_template_page_group_id
        WHERE tp.ae_template_page_group_id = '$tempGroup_id'

NOTE: I have tried all types of join to test without finding a solution.

The PHP

   foreach ($template_pages as $key => $value) {
        ?>
            <li class="dd-item dd3-item" data-id="<? echo $template_pages[$key]['ae_template_page_id']; ?>">
                <div class="dd-handle dd3-handle"></div>
                <div class="dd3-content ae_template_page" data-template_page_name='<? echo $template_pages[$key]['ae_template_page_title']; ?>' data-template_page_id='<? echo $template_pages[$key]['ae_template_page_id']; ?>'><? echo $template_pages[$key]['ae_template_page_title']; ?>
                    <span style='float: right;margin-top: -3px;'>
                        <div style="" class="btn-group">
                            <span class="btn-info btn-xs dropdown-toggle" data-toggle="dropdown" title="Click to change file type" type="button"><? echo $template_pages[$key]['ae_template_pageTypes_type_label']; ?></span>
                                <ul role="menu" class="dropdown-menu" data-page_id='<? echo $template_pages[$key]['ae_template_page_id']; ?>'>
                                    <li><a href="#">JS</a></li>
                                    <li><a href="#">HTML</a></li>
                                    <li><a href="#">CSS</a></li>
                                </ul>
                            <span class="btn-info btn-xs" title="This file belongs to Template Group: <? echo $template_pages[$key]['ae_template_group_name']; ?>" type="button"><? echo $template_pages[$key]['ae_template_group_name']; ?></span>
                            <?
                            if ($template_pages[$key]['is_group_index'] == 1) {
                                ?>
                                <span class="btn-success btn-xs" title="This is the default file for this group" type="button">Group index</span>
                                <?
                            }
                            ?>




                        </div>
                    </span>
                </div>
            </li>
        <?
        }

SQL workbench output using the above statement with the $tempGroupId set to 1 SQL工作台输出

For this and future try and restructure your sql to make it clearer eg:

select * from (
    (select * from table_a) tbla
    left join
    (select * from table_b) tblb
    on tbla.commoncolname=tblb.commoncolname
)

Try this on the database tool first (eg SQL Workbench), then move on to what the php retrieves and so on to the html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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