1

我有多个具有相同结构的 mysql 表。我正在尝试使用单个查询显示所有表的所有结果。我已经在网上搜索了几个小时,这就是我想出的。

    $videos_query = mysql_query("SELECT 
                    table1.title,
                    table1.url,
                    table1.image,
                    table2.title,
                    table2.url,
                    table2.image,
                    table3.title,
                    table3.url,
                    table3.image
                 FROM
                    table1
                 INNER JOIN table2 ON table1.url = table2.url
                 INNER JOIN table3 ON table1.url = table3.url");
while($video_info = mysql_fetch_array($videos_query)){

但是,在 PHP 中使用上面的代码时,我得到了错误“警告:mysql_fetch_array() 期望参数 1 是资源,给定的布尔值”

4

3 回答 3

1

您的 sql 查询中有错误 -table1必须是table2

$videos_query = mysql_query("SELECT 
                    table1.title,
                    table1.url,
                    table1.image,
                    table2.title,
                    table2.url,
                    table2.image,
                    table3.title,
                    table3.url,
                    table3.image
                 FROM
                    table1
                 INNER JOIN table2 ON table1.url = table2.url
                 INNER JOIN table3 ON table1.url = table3.url");

if (mysql_num_rows($videos_query) > 0) {
    while ($video_info = mysql_fetch_array($videos_query)) {
        // process your data here
    }
}

如果你想做联合选择:

$videos_query = mysql_query("
  SELECT title, url, image
  FROM table1
  UNION
  SELECT title, url, image
  FROM table2
  UNION
  SELECT title, url, image
  FROM table3
");

if (mysql_num_rows($videos_query) > 0) {
    while ($video_info = mysql_fetch_array($videos_query)) {
        // process your data here
    }
}

此外,也许您想按某个列排序 - 然后添加ORDER BY+ column_name。

于 2013-10-24T11:41:15.023 回答
1

改变

 INNER JOIN table2 ON table1.url = table1.url

INNER JOIN table2 ON table1.url = table2.url
于 2013-10-24T11:43:24.903 回答
0

在检查ON您正在比较 table1.url = table1.url哪个不正确时。

这是正确的代码:

$videos_query = mysql_query("SELECT 
                table1.title,
                table1.url,
                table1.image,
                table2.title,
                table2.url,
                table2.image,
                table3.title,
                table3.url,
                table3.image
             FROM
                table1
             INNER JOIN table2 ON table1.url = table2.url
             INNER JOIN table3 ON table1.url = table3.url");
while($video_info = mysql_fetch_array($videos_query)){
于 2013-10-24T11:42:53.370 回答