【问题标题】:Trying to join multiple tables in MySQL database and display in Web Table尝试连接 MySQL 数据库中的多个表并在 Web Table 中显示
【发布时间】:2015-06-20 21:39:54
【问题描述】:

好的,所以我对这个数据库的东西很陌生,我正在尝试弄清楚如何同时查询多个表。显然,您可以根据需要查询任意数量的不同表,但如果执行太多连接,可能会遇到性能不佳的情况。但是,我在让 2 个表加入时遇到问题,更不用说 7 个左右的表了,我以后需要加入。

我已经阅读了有关连接表的各种方式的一些信息,并且大多数人似乎更喜欢 union 选项,因为 MySQL 不支持 Full Join。此方法显示的示例不仅让我感到困惑,而且在尝试一次连接 7 个表时看起来会变得非常复杂。然后我看到一篇文章HERE 说MySQL可以使用逗号操作符来模拟全连接。这不仅看起来更容易理解,而且在连接很多表时更容易使用。但我似乎无法让它为我工作,所以希望有人可以帮助我解决这个问题。

编辑 - 这里有更多信息,希望对您有所帮助。

我有两张桌子

test_dogs有品种名称和基本品种信息

lifestyle 作为品种特征,例如狗需要多少运动量、平均健康状况等。

两个表都有一个名为Breed_Name 的列,它是lifestyle 表中的外键。

我想创建一个查询,在这种情况下我可以连接两个表并选择符合以下条件的品种:

Breed_Size = 大

锻炼

我能够连接到我的数据库并对各个表执行查询。

SQL

    $large = $db->query('
SELECT * 
from test_dogs 
where breed_size = "Large" 
order by breed_name '); // this query works

    $exercise = $db->query('
SELECT * 
from lifestyle 
where exercise < 7 
order by exercise DESC'); // this query works

    $join = $db->query(' 
SELECT test_dogs.*, lifestyle.* 
from test_dogs, lifestyle 
ON test_dogs.breed_name = lifestyle.breed_name 
where test_dogs.breed_size = "Large" 
and lifestyle.exercise < 7 
order by exercise DESC'); // THIS QUERY DOES NOT WORK

PHP

<h1> Large Breeds </h1> <!--This table works-->

    <table>
        <tr>
            <th>Breed Name</th>
            <th>Size</th>
        </tr>

        <tr>
        <?php
        while ($rows = $large->fetch()){
            echo "<tr><td>" . $rows['Breed_Name'] . "</td><td>" . $rows['Breed_Size'] . "</td></tr>";
        };
        ?>
    </table>

    <h1> Not High Exercise </h1> <!--This table works-->

    <table>
        <tr>
            <th>Breed Name</th>
            <th>Exercise Needs</th>
        </tr>

        <tr>
        <?php
        while ($rows = $exercise->fetch()){
            echo "<tr><td>" . $rows['Breed_Name'] . "</td><td>" . $rows['Exercise'] . "</td></tr>";
        };
        ?>
    </table>

    <h1> Large AND Not High Exercise </h1> <!--This table DOES NOT work-->

    <table>
        <tr>
            <th>Breed Name</th>
            <th>Size</th>
            <th>Exercise Needs</th>
        </tr>

        <tr>
        <?php
        while ($rows = $join->fetch()){
            echo "<tr><td>" . $rows['test_dogs.Breed_name'] . "</td><td>" . $rows['test_dogs.Breed_Size'] ."</td><td>" . $rows['lifestyle.Exercise'] . "</td></tr>";
        };
        ?>
    </table>

我看到有些人如何包含有关他们尝试访问的数据库的信息,但我不知道该怎么做。如果我可以提供更多信息来帮助使这个问题更清楚,请告诉我。

【问题讨论】:

  • 请解释您想要的连接结果。目前还不清楚您要达到的目标。
  • 联合和连接是两个完全不同的东西。
  • @SamiKuhmonen - 我想这有点不清楚。我试图从每个数据库表中提取某些元素,将结果存储在一个变量中,然后使用该变量在网页上创建一个 php 表。这基本上是学习如何创建搜索引擎的第一步,我的网站访问者可以使用它来搜索我的数据库。
  • 您有点混合了隐式(逗号)和显式 JOIN 样式。不。如果事实上,根本不要使用逗号连接。

标签: php mysql database join


【解决方案1】:

只是一个关于你可以在表http://blog.codinghorror.com/a-visual-explanation-of-sql-joins/之间拥有的各种类型的连接的小指针

参考您说的上面的 sql 不起作用-我个人发现语法不清楚,这在您尝试学习时对您没有帮助。在我看来,其他人可能会不同意,更清晰的语法类似于:-

select * from `test_dogs` td
    left outer join `lifestyle` l on l.`breed_name`=td.`breed_name`
    where 
        td.`breed_size`='Large'
        and
        l.`exercise` < '7'
    order by l.`exercise` desc;

表名后面的 td 和 l 称为别名,允许您通过该唯一标识符始终引用表。有了别名后,您可以直接使用别名和字段名称引用特定字段,而不是选择所有记录(用 * 表示)。即:td.dog_type 等,因此查询可能更具体:-

select 
    td.`dog_type` as 'type',
    td.`dog_weight` as 'weight',
    l.`walks_per_day` as 'walkies'
    from `test_dogs` td
        left outer join `lifestyle` l on l.`breed_name`=td.`breed_name`
        where 
            td.`breed_size`='Large'
            and
            l.`exercise` < '7'
        order by l.`exercise` desc;

等等......如果你愿意,使用“as 'type'”可以让你为引用的字段提供一个简短的名字。

在没有看到您的数据库的情况下,我们无法明确说明您的查询不起作用的原因 - 请确保您尝试链接表的列包含常见信息

【讨论】:

  • 非常感谢您的回答。我一定会尽快尝试您的建议(很快就会开始工作)。需要明确的是,你在这里说的是我的语法有点不对劲?我注意到一些“”在我的示例中没有。此外,“公共信息”的概念让我感到困惑。我在其他教程中看到过它,但我没有理解它。 “公共信息”究竟是什么意思?
  • @shirley - 很抱歉没有尽快回复,我已经离开了,刚刚回来。很高兴你解决了这个问题
【解决方案2】:

好的,所以我想通了。 RamRaider 在指出我的语法问题方面非常有帮助,我会接受他的回答。我在这里发布这个是为了那些可能理解我的意思并在同一件事上需要帮助的人。

所以我试图弄清楚如何编写我的 $db->查询,以便我可以一次查询 2 个表。我的test_dogs 表和lifestyle 表。事实证明,正如 RamRaider 指出的那样,我错过了引文。正确的语法是:

SQL

SELECT td.`breed_name`, td.`breed_size`, l.`Exercise` 
FROM test_Dogs as td 
LEFT OUTER JOIN Lifestyle as l 
ON td.`Breed_Name` = l.`Breed_Name` 
WHERE td.`breed_size` like "%Large%" 
AND l.`exercise` < 7 
ORDER BY l.`Exercise` ASC'

我将 ORDER BY 从降序 (DESC) 更改为升序 (ASC),因为我认为这样更有意义。

然后,我想用我的查询结果填充一个表。如下所示:

PHP

<table>
        <tr>
            <th>Breed Name</th>
            <th>Size</th>
            <th>Exercise Needs</th>
        </tr>

        <tr>
        <?php
        while ($rows = $join->fetch()){
            echo "<tr><td>" . $rows['breed_name'] . "</td><td>" . $rows['breed_size'] ."</td><td>" . $rows['Exercise'] . "</td></tr>";
        };
        ?>
    </table>

在让 SQL 工作后我发现,无论我在查询中使用什么大写,我都需要在 php 中使用相同的大写,否则它将不起作用。请注意,在我的 SELECT 部分的 SQL 中,我将前 2 列写成小写,但奇怪的是决定将最后一列大写。然后,当我在 PHP 中使用全小写时,它不起作用,花了一些时间才弄清楚原因。另外,我意识到我不需要指定每列来自哪个表。或者至少在这种情况下不是。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多