尝试联接 MySQL 数据库中的多个表并显示在 Web 表中


Trying to join multiple tables in MySQL database and display in Web Table

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

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

编辑 - 这里有一些更多信息,希望它会有所帮助。

我有两张桌子

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

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

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

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

Breed_Size = 大

锻炼<7(生活方式表中的特征为 1 - 10)

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

.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>

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

只是一个关于表之间可以有各种类型的连接的指针 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 称为别名,并允许您始终通过该唯一标识符引用表。获得别名后,无需选择所有记录(用 * 表示),您可以使用别名和字段名称直接引用特定字段。即:道明。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;

等。。。。使用"作为'类型'"允许您根据需要为引用字段提供简短的名字对象。

如果没有看到您的数据库,我们无法明确说明您的查询不起作用的原因 - 确保您尝试链接表的列包含通用信息

好的,所以我弄清楚了。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 中使用相同的大写,否则它将不起作用。请注意,在我的 SQL 中的 SELECT 部分中,我用小写写了前 2 列,但奇怪的是决定将最后一列大写。然后,当我在 PHP 中使用所有小写字母时,它不起作用,并且需要一些时间来弄清楚原因。另外,我意识到我不需要指定每列来自哪个表。或者至少在这种情况下不是。