如何从2个表中获取信息到一个SQL查询?

时间:2014-01-28 13:30:23

标签: mysql sql union union-all

我有这两个表,我需要船和图像信息来显示在相同的sql / loop中。

这可能吗? 内连接?

$info1 = mysql_query(" SELECT image1 as image, boat1 as boat FROM all_images ");
$info2 = mysql_query(" SELECT image2 as image, boat2 as boat FROM all_boats_images ");

while($b = mysql_fetch_array($????????)){
 echo $b['boat'].$b['boat'];
} 

3 个答案:

答案 0 :(得分:1)

只需在表名

之前附加数据库名称

即。如果数据库名称是db1,表名是tb1那么

          SELECT * FROM db1.tb1;

答案 1 :(得分:0)

希望这会有所帮助:

 $query = mysql_query("SELECT all_images.image1, all_boats_images.image2 AS image FROM all_images, all_boat_images");

 while($b = mysql_fetch_array($query))
    {
      echo $b['boat'];
    }

要缩小搜索范围,请尝试将$ query更改为:

$query = mysql_query("SELECT all_images.image1, all_boats_images.image2 AS image FROM all_images, all_boat_images WHERE all_images.id = all_boats_images.id AND all_images.id = whatever_id_you_are_searching_for");

答案 2 :(得分:0)

我认为你想使用联盟,因为你评论说这些表是不相关的

SELECT image1 as image, boat1 as boat FROM all_images
union all
SELECT image2 as image, boat2 as boat FROM all_boats_images