MySQL从多个表中选择

时间:2014-04-07 08:29:50

标签: php mysql select join

我试图从多个表中选择数据,现在我可以使用两个表进行,因为我这样做了一个查询:

$myquery = sql_query(
    "SELECT a.object_title, a.published_by, b.userid
     FROM table1 AS a 
     JOIN table2 AS b ON (a.published_by = b.userid)"
);

但是现在看,我想从第三个表中选择数据,但是这个第三个表没有关系,比如前两个表之间的主键,所以我只是想从中提取数据并形成任何类型的链接到"加入"。

如何简单地将第三个添加到此查询?

由于

3 个答案:

答案 0 :(得分:2)

您可以使用CROSS JOIN

$myquery = sql_query(
    "SELECT a.object_title, a.published_by, b.userid, c.whatever
     FROM table1 AS a 
     JOIN table2 AS b ON (a.published_by = b.userid)
     CROSS JOIN table3 AS c"
);

我用this other post找到了这个想法。

更多信息here

答案 1 :(得分:0)

在你的内部添加查询左连接。

答案 2 :(得分:0)

$myquery = sql_query(
            "SELECT a.object_title, a.published_by, b.userid, c.column_name
             FROM Table1 AS a 
             JOIN Table2 AS b ON (a.published_by = b.userid)
             CROSS JOIN Table3 AS c"
);