连接两个表,只输出第一个表中存在的行

时间:2014-04-14 11:31:29

标签: php mysql join

我正在制作一个允许用户申请多个奖项类别的表单。他们可以申请所有几个类别,但只能申请一次。

奖项类别存储在此表中: enter image description here

当用户申请某个类别时,会使用用户的nominee_id和award_category_id在下表中输入一行: enter image description here

然后我就是这段代码向用户显示他们已经申请了哪些奖项,这很好。

$result = mysql_query("                         
                        SELECT 
                            award_subsection.*,
                            award_subsection.title AS subsection_title,
                            award_nomination_category.*
                        FROM 
                            award_subsection INNER JOIN
                            award_nomination_category
                        ON
                            award_subsection.id=award_nomination_category.award_category_id
                        WHERE
                            award_subsection.active='1' AND
                            award_subsection.award_id='$id' AND
                            award_subsection.additional_function='award category' AND
                            award_nomination_category.active='1' AND
                            award_nomination_category.award_id='$id' AND
                            award_nomination_category.nominee_id='$nominee_id'
                    ");
                    $category_count = mysql_num_rows($result);
                    if($category_count > 0) {
                        echo'
                        <div id="column_full" class="reverse_margin">
                            <div id="subsection_heading">
                                <h3>You have already selected the following award categories:</h3>
                            </div><!--
                            --><div id="subsection_content">
                                <!--
                        ';
                        while ($row = mysql_fetch_array($result)) {
                            echo '
                                    --><div id="column_third">
                                        <h3>' . $row['subsection_title'] . '</h3>
                                    </div><!--
                            ';
                        }
                        echo'
                                    -->
                                </div>
                            </div>
                        ';
                    }

但我的问题是当我还想渲染一个只显示用户未应用的类别的下拉菜单时。以下代码适用于用户之前仅申请过1个类别,但当用户申请的数量超过1时,下拉菜单会显示所有类别。我怀疑这是因为因为同一个用户有两行应用程序,所以脚本在表中运行两次并获得所有类别。

 $result = mysql_query("                            
SELECT 
    award_subsection.*,
    award_subsection.id AS subsection_id,
    award_subsection.title AS subsection_title,
    award_nomination_category.*
FROM 
    award_subsection JOIN
    award_nomination_category
ON
    award_subsection.id!=award_nomination_category.award_category_id
WHERE
    award_subsection.active='1' AND
    award_subsection.award_id='$id' AND
    award_subsection.additional_function='award category' AND
    award_nomination_category.active='1' AND
    award_nomination_category.award_id='$id' AND
    award_nomination_category.nominee_id='$nominee_id'
GROUP BY
    award_subsection.id
 ");
 while($row = mysql_fetch_array($result)) {
echo '
    <option value="' . $row['subsection_id'] . '">' . $row['subsection_title'] . '</option>
';
 }

有没有办法让php知道在第一个查询中选择了哪些类别并在第二个查询中删除它们? Thx提前!

1 个答案:

答案 0 :(得分:0)

我明白了!!

以下是代码,如果有人感兴趣的话:

                        // Check for applied categories
                    $result = mysql_query("                         
                        SELECT 
                            award_subsection.*,
                            award_subsection.id AS subsection_id,
                            award_subsection.title AS subsection_title,
                            award_nomination_category.*
                        FROM 
                            award_subsection INNER JOIN
                            award_nomination_category
                        ON
                            award_subsection.id=award_nomination_category.award_category_id
                        WHERE
                            award_subsection.active='1' AND
                            award_subsection.award_id='$id' AND
                            award_subsection.additional_function='award category' AND
                            award_nomination_category.active='1' AND
                            award_nomination_category.award_id='$id' AND
                            award_nomination_category.nominee_id='$nominee_id'
                    ");
                    $category_count = mysql_num_rows($result);
                    if($category_count > 0) {
                        echo'
                        <div id="column_full" class="reverse_margin">
                            <div id="subsection_heading">
                                <h3>You have already selected the following award categories:</h3>
                            </div><!--
                            --><div id="subsection_content">
                                <!--
                        ';
                        // Create array of IDs that the nominee has already applied for
                        $selected_categories = array();
                        while ($row = mysql_fetch_array($result)) {
                            $selected_categories[] = $row['subsection_id'];
                            echo '
                                    --><div id="column_third">
                                        <h3>' . $row['subsection_title'] . '</h3>
                                    </div><!--
                            ';
                        }
                        echo'
                                    -->
                                </div>
                            </div>
                        ';
                    }
                    echo'
                        <form method="post" action="award-nomination-submit.php" data-parsley-validate>
                            <div id="column_full" class="reverse_margin">                           
                                <input type="hidden" name="award_id" value="' . $id . '">
                                <input type="hidden" name="nominee_id" value="' . $nominee_id . '">
                                <input type="hidden" name="step" value="' . $step . '">

                                <h3>Select an Award Category:</h3>
                                <select class="visual_form" name="award_category"  required>
                                    <option value="" selected>Please Select</option>
                    ';
                    // Build award categories drop down
                    $result = mysql_query("                         
                        SELECT 
                            *
                        FROM 
                            award_subsection
                        WHERE
                            active='1' AND
                            award_id='$id' AND
                            additional_function='award category'
                    ");
                    while($row = mysql_fetch_array($result)) {
                        // If ID exists in array, don't echo out in drop down
                        if(in_array($row['id'], $selected_categories)) {
                            echo'';
                        }
                        else {
                            echo '
                                <option value="' . $row['id'] . '">' . $row['title'] . '</option>
                            ';
                        }
                    }
                    echo '
                                    </select>
                                </div>
                    ';

基本上,我使用从第一个sql查询输出的ID创建了一个数组。然后在第二个sql查询中,我将ID与数组中的ID进行了比较,并且只回显了那些不匹配的内容:)