简体   繁体   English

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

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

Ok, so I'm very new to this database stuff, and I'm trying to figure out how to query multiple tables at the same time.好的,所以我对这个数据库的东西很陌生,我试图弄清楚如何同时查询多个表。 Apparently you can query as many different tables as you'd like, although you might experience poor performance if you do too many joins.显然,您可以根据需要查询任意数量的不同表,但如果执行过多连接,可能会导致性能不佳。 However I'm having issues getting 2 tables to join, let alone the 7 or so I'd need to join later.但是,我在加入 2 个表时遇到了问题,更不用说我稍后需要加入的 7 个左右的表了。

I've done some reading on the various ways you can join tables, and most people seem to favor the union option, since MySQL doesn't support the Full Join .我已经阅读了可以连接表的各种方式,大多数人似乎更喜欢union选项,因为 MySQL 不支持Full Join The examples shown for this method not only confuse me, but look like it would get massively complicated when trying to join 7 tables at once.为这种方法显示的示例不仅让我感到困惑,而且在尝试一次连接 7 个表时看起来会变得非常复杂。 Then I saw an article HERE that says MySQL can use a comma operator that simulates a full join.然后我在这里看到一篇文章,说 MySQL 可以使用逗号运算符来模拟完全连接。 This not only looked much easier to understand, but much easier to use when joining very many tables.这不仅看起来更容易理解,而且在连接非常多的表时更容易使用。 But I can't seem to get it to work for me, so hopefully someone can help me get this figured out.但我似乎无法让它为我工作,所以希望有人能帮助我解决这个问题。

EDIT - Here is some more information, hopefully it will help.编辑- 这里有更多信息,希望它会有所帮助。

I have two tables我有两张桌子

test_dogs which has breed names and basic breed information test_dogs具有品种名称和基本品种信息

lifestyle which as breed characteristics such as how much exercise a dog needs, average health and such. lifestyle作为品种特征,例如狗需要多少运动,平均健康状况等。

Both tables have a column named Breed_Name , which is the foreign key in the lifestyle table.两个表都有一个名为Breed_Name的列,它是lifestyle表中的外键。

I want to create a query where in this case I can join the two tables and select breeds that fit the following conditions:我想创建一个查询,在这种情况下,我可以连接两个表并选择符合以下条件的品种:

Breed_Size = Large Breed_Size = 大

Exercise < 7 (traits in the lifestyle table are on a scale of 1 - 10)练习 < 7(生活方式表中的特征按 1 - 10 的等级划分)

I am able to connect to my database and perform queries on individual tables.我能够连接到我的数据库并对单个表执行查询。

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

I've seen how some people can include information about the database they're trying to access, but I don't know how to do that.我已经看到有些人如何包含有关他们试图访问的数据库的信息,但我不知道如何做到这一点。 If there's any more information I could offer to help make this question clearer, please let me know.如果我可以提供更多信息来帮助使这个问题更清楚,请告诉我。

just a wee pointer as to the various types of joins you can have between tableshttp://blog.codinghorror.com/a-visual-explanation-of-sql-joins/只是一个关于表之间可以有的各种类型的连接的小指针http://blog.codinghorror.com/a-visual-explanation-of-sql-joins/

With reference to your sql above that you say does not work - personally I find that syntax is unclear which doesn't help you at this point when you are trying to learn.参考您上面的 sql,您说它不起作用 - 我个人发现语法不清楚,这在您尝试学习时对您没有帮助。 To my mind, and others will likely disagree, a clearer syntax would be something akin to:-在我看来,其他人可能会不同意,更清晰的语法类似于:-

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;

Where td and l after the table names are known as aliases and allow you reference the table by that unique identifier throughout.其中表名后面的 td 和 l 被称为别名,并允许您在整个过程中通过该唯一标识符引用该表。 Rather than selecting all records ( indicated with * ) once you have the alias you can reference specific fields directly using the alias and the field name.有了别名后,您可以直接使用别名和字段名称引用特定字段,而不是选择所有记录(用 * 表示)。 ie: td.即:td。 dog_type etc so the query could be more specific:- 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;

etc.... the use of " as 'type'" allows you to supply a short moniker to the referenced field if you wish.等等.... 使用 " as 'type'" 允许您根据需要为引用的字段提供一个简短的名字。

Without seeing your database we cannot categorically state why your query does not work - make sure the columns you are trying to link tables with contain common information在没有看到您的数据库的情况下,我们无法明确说明为什么您的查询不起作用 - 确保您尝试链接表的列包含公共信息

Ok, so I got this figured out.好的,所以我明白了。 RamRaider was very helpful in pointing out the issues with my syntax, and I will be accepting his answer. RamRaider 在指出我的语法问题方面非常有帮助,我将接受他的回答。 I'm posting this here for those who might understand what I was getting at and wanted help with the same thing.我在这里发布这个是为了那些可能理解我的意思并在同样的事情上需要帮助的人。

So I was trying to figure out how to write my $db->query so that I could query 2 tables at once.所以我试图弄清楚如何编写我的 $db->query 以便我可以一次查询 2 个表。 My test_dogs table and lifestyle table.我的test_dogs表和lifestyle表。 It turns out, as RamRaider pointed out, I was missing quotations.事实证明,正如 RamRaider 指出的那样,我缺少引用。 The proper syntax was:正确的语法是:

SQL 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'

I changed the ORDER BY from Descending ( DESC ) to Ascending ( ASC ), as I decided that made more sense.我将ORDER BY从降序 ( DESC ) 更改为升序 ( ASC ),因为我认为这更有意义。

Then, I wanted to populate a table with the results of my query.然后,我想用我的查询结果填充一个表。 Which looked as follows:如下所示:

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

What I have discovered after getting the SQL to work is that whatever capitalization I use in the query, I need to use the same capitalization in the php, or it will not work.在让 SQL 工作后我发现无论我在查询中使用什么大写,我都需要在 php 中使用相同的大写,否则它将不起作用。 Notice in my SQL in the SELECT section, I wrote the first 2 columns in lower-case, but oddly decided to capitalize the last column.请注意,在 SELECT 部分的 SQL 中,我以小写形式编写了前 2 列,但奇怪的是决定将最后一列大写。 Then, when I used all lower-case in the PHP, it didn't work, and it took some time to figure out why.然后,当我在PHP中使用全部小写时,它不起作用,花了一些时间才弄清楚原因。 Also, I realized I did not need to specify which table each column came from.此外,我意识到我不需要指定每列来自哪个表。 Or at least not in this instance.或者至少在这种情况下不是。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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