简体   繁体   English

3桌内联

[英]Inner join from 3 table

I have 3 tables : 我有3张桌子:

tb_wrapper              tb_doc                 tb_aut
=====================   ====================   =======================
|id|web|title|source|   |id| web | content |   |id|web|aut  | linkAut|
=====================   ====================   =======================  
|1 | A |  AA | AAA  |   |1 | A   |  AAAA   |   |1 | A | B   | C      |
=====================   ====================   |2 | A | D   | E      |
                                               =======================

I wanna get all of record data from 3 table that has same web . 我想从具有相同web 3个表中获取所有记录数据。

here's my code : 这是我的代码:

$query = mysql_query("
                     SELECT 
                        tb_wrapper.web,
                        tb_wrapper.title,
                        tb_wrapper.source,
                        tb_doc.web,
                        tb_doc.content,
                        tb_aut.web,
                        tb_aut.aut,
                        tb_aut.linkAut
                     FROM
                        tb_doc
                     INNER JOIN tb_wrapper ON tb_doc.web = tb_wrapper.web 
                        AND
                     INNER JOIN tb_aut ON tb_doc.web = tb_aut.web 
 ");

The error problem : Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in D:\\AppServ\\www\\sukses\\indexsummary.php on line 87 . 错误问题: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in D:\\AppServ\\www\\sukses\\indexsummary.php on line 87

EDIT 编辑

Then I wanna print them all : 然后我想将它们全部打印出来:

while ($row = mysql_fetch_array($query)) {
    $source  = $row['source'];
    $title   = $row['title'];
    $content = $row['content'];
    $aut  = $row['aut'];
    $linkAut   = $row['linkAut'];
echo '<h2><a href='.$source.'> '.$title.' </a></h2>';
echo '<p><a href='.$aut_url.'> '.$aut.'  </a></p>';
}

And the result, they are printed twice. 结果,它们被打印了两次。 AA B and AA D. How to make them AA BD ? AA B和AAD。如何使它们成为AA BD?

Please help me. 请帮我。 thanks in advance :) 提前致谢 :)

$query = mysql_query("
                     SELECT 
                        tb_wrapper.web,
                        tb_wrapper.title,
                        tb_wrapper.source,
                        tb_doc.web,
                        tb_doc.content,
                        tb_aut.web,
                        tb_aut.aut,
                        tb_aut.linkAut
                     FROM
                        tb_doc
                     INNER JOIN tb_wrapper ON tb_doc.web = tb_wrapper.web
                     INNER JOIN tb_aut ON tb_doc.web = tb_aut.web 

YOu need to remove the AND between inner joins, that's invalid syntax. 您需要删除内部联接之间的AND,这是无效的语法。

You probably need to go do some more reading about how JOINS work in SQL. 您可能需要阅读更多有关JOINS如何在SQL中工作的信息。 You seem to want to gather results from multiple rows, and that's generally not how it works. 您似乎想从多行中收集结果,通常这不是它的工作方式。

If you're joining on a non-unique column from each table, you should expect to get repeated results for rows that have repeated values. 如果要在每个表的非唯一列上联接,则应该期望具有重复值的行会得到重复结果。

I may have not explained that properly, so more detail below. 我可能没有适当地解释这一点,因此请在下面进行详细说明。 Here are your tables again as reference: 这又是您的表格作为参考:

tb_wrapper              tb_doc                 tb_aut
=====================   ====================   =======================
|id|web|title|source|   |id| web | content |   |id|web|aut  | linkAut|
=====================   ====================   =======================  
|1 | A |  AA | AAA  |   |1 | A   |  AAAA   |   |1 | A | B   | C      |
=====================   ====================   |2 | A | D   | E      |
                                               =======================

Your query (without the AND as others have suggested) joins the 'web' column of each of the tables. 您的查询(没有其他人建议的AND)将联接每个表的'web'列。

If you just look at your first 2 tables, they have only 1 row and 'web' is A. If you only joined these tables, you would get a 1-row result: 如果仅查看前两个表,则它们只有1行,并且'web'为A。如果仅连接这些表,则会得到1行结果:

==================================
|web|title|source| web | content |
==================================
| A |  AA | AAA  | A   |  AAAA   |
==================================

But your third table has 2 rows with the same 'web' value. 但是您的第三个表有2行,具有相同的“ web”值。 The 1-row result from joining the first 2 tables can be joined to both rows of your third table. 联接前2个表的1行结果可以联接到第三个表的两行。 This produces the following result: 这将产生以下结果:

=====================================================
|web|title|source| web | content |web|aut  | linkAut|
=====================================================  
| A |  AA | AAA  | A   |  AAAA   | A | B   | C      |
| A |  AA | AAA  | A   |  AAAA   | A | D   | E      |
=====================================================

Your PHP then loops through the results and prints 2 columns ('title' and 'aut') from each row, resulting in: 然后,您的PHP遍历结果,并从每一行打印2列(“ title”和“ aut”),结果是:

AA  B
AA  D

This is exactly how it's supposed to work. 这正是它应该如何工作的。 If you want to get 'AA BD' as a result, simple iteration through rows will not be sufficient. 如果要获得“ AA BD”的结果,则仅通过行进行迭代是不够的。

To see how it changes when values are unique, join all your rows on the 'id' column instead. 要查看值唯一时的变化,请在“ id”列上连接所有行。 Since each row's 'id' is unique in your sample, you will only get 1 row in your result. 由于每行的“ id”在您的示例中都是唯一的,因此您的结果将仅获得1行。

If you're instead trying to get values returned from different rows (like showing the Title and Content once, but then a list of 'aut' values), you need to use 2 different queries or put some additional logic in your while loop. 如果您想获取从不同行返回的值(例如一次显示“标题”和“内容”,然后显示“ aut”值的列表),则需要使用2个不同的查询或在while循环中添加一些其他逻辑。

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

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