简体   繁体   English

MySQL 从多个表中选择

[英]MySQL selecting from multiple tables

im trying to select data from multiple tables, now i am fine with doing it with two tables as i do a query like so:我试图从多个表中选择数据,现在我可以用两个表来做,因为我做这样的查询:

$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)"
);

But see now, i want to select data from a third table, however this third table does not have relationship such as primary key between the first two tables, so i simply want to just pull data from it and form any sort of link with a "JOIN".但是现在看,我想从第三个表中选择数据,但是这个第三个表没有前两个表之间的主键等关系,所以我只想从中提取数据并形成任何类型的链接“加入”。

How would simply add the third to this query?如何简单地将第三个添加到此查询中?

Thanks谢谢

You could use CROSS JOIN :您可以使用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"
);

I used this other post to find the idea.我用另一个帖子找到了这个想法。

More infos here .更多信息在这里

在您的查询中添加左连接。

$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"
);

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

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