运行 MySQL 查询,存储临时值,然后运行另一个查询

发布于 2024-11-07 08:27:11 字数 1547 浏览 3 评论 0原文

为了提供一些上下文,以下下拉列表位于一个表单中,该表单给出了数据库中存储的动物的当前配置文件描述:即,这匹小马很高,属于这种性别,属于某个人,等等。该表单的目的是编辑这些当前值。有多种选项可以自由覆盖,其他选项我希望限制为下拉菜单中的选项。

以下是我用于“性别”字段的当前代码,尽管它确实有效,但它并不是正确的处理方式。我对查询当前状态的方法更感兴趣,将当前状态作为默认选项,存储当前状态,查询不等于存储的当前状态的其他可用状态,然后将剩余状态作为选项。这种方法是我最能适应表单上所有其他下拉菜单的方法。

引用了两个表 - 配置文件 tbl 和 prm_breedgender tbl。每个性别都有一个 ID,然后每个个人资料都有一个相应的 ID 来表示其性别(男性 = 1,女性 = 2 等)。 $profile 变量表示当前正在查看的配置文件。

<label for="profile-gender">Gender / Type:</label>
    <select name="profile-gender">
        <?php
            $genderresult = mysql_query("SELECT prm_breedgender.BreedGender
                            FROM profiles, prm_breedgender
                            WHERE ProfileID = $profile
                            AND prm_breedgender.BreedGenderID = profiles.ProfileGenderID");
            $row = mysql_fetch_array($genderresult);
            echo '<option value="' . $row['BreedGenderID'] . '">' . $row[BreedGender] . '</option>';

            $exgenderresult = mysql_query("SELECT prm_breedgender.BreedGender
                            FROM profiles, prm_breedgender
                            WHERE ProfileID = $profile
                            AND prm_breedgender.BreedGenderID != profiles.ProfileGenderID");
            while ($row = mysql_fetch_array($exgenderresult)) {
            echo '<option value="' . $row['BreedGenderID'] . '">' . $row[BreedGender] . '</option>';
            }
        ?>
    </select>

任何帮助将不胜感激。我的经验并不丰富(显然!),所以附带的解释会很棒。

To give some context, the following dropdown is within a form that gives the current profile description of an animal stored in the database: i.e, this pony is yea high, of this gender, owned by somesuch, etc. The purpose of the form is to edit these current values. There are various options that can be overwitten freely, others where I want the choice limited to options from a dropdown menu.

The following is the current code I am using for the Gender field, which - although it does work - cannot be the proper way of doing things. I would be more interested in a method that queried the current state, gave the current state as the default option, stored the current state, queried other available states not equal to the stored current state, then gave the remaining states as options. It is that method that I could best adapt to all the other dropdowns on the form.

There are two tables referenced - the profile tbl and the prm_breedgender tbl. Each gender is given an ID, each profile is then given a corresponding ID to signify their gender (male=1, female=2, etc.). The $profile variable is that which signifies the current profile being looked at.

<label for="profile-gender">Gender / Type:</label>
    <select name="profile-gender">
        <?php
            $genderresult = mysql_query("SELECT prm_breedgender.BreedGender
                            FROM profiles, prm_breedgender
                            WHERE ProfileID = $profile
                            AND prm_breedgender.BreedGenderID = profiles.ProfileGenderID");
            $row = mysql_fetch_array($genderresult);
            echo '<option value="' . $row['BreedGenderID'] . '">' . $row[BreedGender] . '</option>';

            $exgenderresult = mysql_query("SELECT prm_breedgender.BreedGender
                            FROM profiles, prm_breedgender
                            WHERE ProfileID = $profile
                            AND prm_breedgender.BreedGenderID != profiles.ProfileGenderID");
            while ($row = mysql_fetch_array($exgenderresult)) {
            echo '<option value="' . $row['BreedGenderID'] . '">' . $row[BreedGender] . '</option>';
            }
        ?>
    </select>

Any help would be greatly appreciated. I'm not all that experience (obviously!) so accompanying explainations would be fantastic.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绝不服输 2024-11-14 08:27:11

您可以使用计算的“order by”值将其组合成单个查询:

SELECT ...
FROM ...
WHERE ...
ORDER BY (prm_breedgender.BreedGenderID = profiles.ProfileGenderID) DESC

order by 子句将评估为 true(您位于应该是第一个的记录上)或 false(任何其他记录),这将被区分到一个可以排序的值 1/0,并按降序排序,真正的值 (1) 首先出现。

You can combine it into a single query, using a computed 'order by' value:

SELECT ...
FROM ...
WHERE ...
ORDER BY (prm_breedgender.BreedGenderID = profiles.ProfileGenderID) DESC

The order by clause will evaluate to either true (you're on the record that should be first) or false (any other record), which gets cased to a value 1/0 which can be sorted, and by doing it in decreasing order, the true values (1) come out first.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文