简体   繁体   English

MySQL查询从两个表中提取,显示在正确的字段中

[英]MySQL query pulling from two tables, display in correct fields

I'm trying to select all fields in two separate tables as long as they're sharing a common ID. 我试图在两个单独的表中选择所有字段,只要它们共享一个公共ID。

//mysql query
$result = mysql_query("SELECT * FROM project, links 
WHERE project.id = links.id and project.id = $clientID")

//displaying the link 
if ($row['url'] != null){
    echo "<div class='clientsection' id='links'>Links</div>";
    echo "<a class='clientlink' id='link1' href='" . $row['url'] . "'>" . $row['name'] . "</a>";
}
else {
echo "<a class='clientlink' id='link1' href='" . $row['url'] . "' style='display:none;'>" . $row['name'] . "</a>";
        };

As you can see, my tables are "projects", and "links" Each is sharing a common field "id" for reference. 如您所见,我的表是“项目”和“链接”,每个表都共享一个公共字段“ id”以供参考。 It looks as though where both links.id and project.id are equal, it outputs anything, but when there is no links.id associated with a given $clientID the container relative to the $clientID doesn't display at all. 看起来好像links.id and project.id都相等,它会输出任何内容,但是当没有与给定$clientID关联的links.id相对于$clientID的容器根本不会显示。

Essentially I'm using this to add links dynamically to a specific client in this CMS and if there are no links, I want the container to show up anyway. 本质上,我正在使用它来动态添加到此CMS中特定客户端的链接,如果没有链接,无论如何我都希望容器显示出来。

Hopefully I've expressed myself clearly, any pointers in the right direction are appreciated. 希望我已经清楚表达了自己的观点,对正确方向的任何指示都表示赞赏。 Thanks! 谢谢!

Using the SQL ANSI explicit join syntax, you can specify a LEFT OUTER JOIN , as opposed to the INNER JOIN that you are doing in your implicit join. 使用SQL ANSI显式联接语法,可以指定LEFT OUTER JOIN ,而不是在隐式INNER JOIN中进行的INNER JOIN

The LEFT OUTER JOIN returns results in the first table even if there are no matching rows in the joining table: 即使联接表中没有匹配的行,LEFT OUTER JOIN也会在第一个表中返回结果:

SELECT * FROM project
LEFT OUTER JOIN links 
ON links.id = project.id
WHERE project.id = $clientID

For rows where there is no matching id in the links table, links.id will be NULL . 对于链接表中没有匹配ID的行,links.id将为NULL

Edit 编辑

If you want just the first link, where the autoincrement primary key links.linkid specifies the order, you can do a self join like this: 如果只需要第一个链接,即自动递增主键links.linkid指定顺序,则可以这样进行自我连接:

SELECT * FROM project
LEFT OUTER JOIN links
ON links.id = project.id
LEFT OUTER JOIN links l2
ON l2.id = project.id AND l2.linkid < links.linkid
WHERE project.id = $clientID AND l2.id IS NULL

What this does is join in any links table rows where the linkid is smaller, and then exludes those rows in the WHERE clause. 这样做是在linkid较小的任何链接表行中进行联接,然后在WHERE子句中排除这些行。 You end up with just the rows where there is no smaller linkid, hence the link with the smallest linkid for each project. 您最终只得到没有较小linkid的行,因此每个项目的linkid最小的链接。

You're query is essentially doing an "INNER JOIN" so it will only return records where there is matching id's in both tables. 您所查询的实际上是在执行“ INNER JOIN”,因此它将仅返回两个表中具有匹配ID的记录。 If you do an "LEFT OUTER JOIN" as in Marcus's example it will return the results you are looking for. 如果像Marcus的示例中那样进行“ LEFT OUTER JOIN”,它将返回您想要的结果。

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

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