3 个表,左连接,以防一个表中缺少数据

发布于 2024-10-16 08:34:51 字数 1152 浏览 7 评论 0原文

table1 (ids 始终存在)

+----+------+
| id | col1 |
+----+------+
| 1  |  ab  |
+----+------+
| 2  |  gh  |
+----+------+

table2 (ids 始终存在)

+----+------+
| id | col2 |
+----+------+
| 1  |  cd  |
+----+------+
| 2  |  ij  |
+----+------+

table3 (ids 可能会丢失,在本例中 2 丢失)

+----+------+
| id | col3 |
+----+------+
| 1  |  ef  |
+----+------+

PHP

$col = 'ab';

$a = mysql_query("SELECT t1.id FROM table1 AS t1, table2 AS t2, table3 AS t3
WHERE t1.id = t2.id AND t2.id = t3.id AND (t1.col1 = '$col' OR t2.col2 = '$col'
OR t3.col3 = '$col) GROUP BY t1.id, t2.id, t3.id");

仅当所有三个表都包含“相同的 id”时才有效,但是如果 table3 由于某种原因缺少“id”,会发生什么情况?当 $col = ab 时,如何仍然测试所有三个表并让 t1.id 输出 1?我必须使用左连接吗?

$a = mysql_query("SELECT t1.id FROM table1 AS t1, table2 AS t2 
LEFT JOIN (SELECT id FROM table3 WHERE col3 = '$col') AS t3 ON t3.id = t1.id 
WHERE t1.id = t2.id AND (t1.col1 = '$col' OR t2.col2 = '$col')
GROUP BY t1.id, t2.id");

我在这里做错了什么?

table1 (ids always exist)

+----+------+
| id | col1 |
+----+------+
| 1  |  ab  |
+----+------+
| 2  |  gh  |
+----+------+

table2 (ids always exist)

+----+------+
| id | col2 |
+----+------+
| 1  |  cd  |
+----+------+
| 2  |  ij  |
+----+------+

table3 (ids might be missing, in this case 2 is missing)

+----+------+
| id | col3 |
+----+------+
| 1  |  ef  |
+----+------+

PHP

$col = 'ab';

$a = mysql_query("SELECT t1.id FROM table1 AS t1, table2 AS t2, table3 AS t3
WHERE t1.id = t2.id AND t2.id = t3.id AND (t1.col1 = '$col' OR t2.col2 = '$col'
OR t3.col3 = '$col) GROUP BY t1.id, t2.id, t3.id");

That would only work if all three tables had "the same id" included, but what happens if an "id" is missing in table3 for whatever reason? how can I still test for all three tables and get t1.id to output 1, when $col = ab? would I have to use left join?

$a = mysql_query("SELECT t1.id FROM table1 AS t1, table2 AS t2 
LEFT JOIN (SELECT id FROM table3 WHERE col3 = '$col') AS t3 ON t3.id = t1.id 
WHERE t1.id = t2.id AND (t1.col1 = '$col' OR t2.col2 = '$col')
GROUP BY t1.id, t2.id");

what am I doing wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

流星番茄 2024-10-23 08:34:51

你做错了什么?查询不存在的表。这总是会引发错误。

我不会讨论设计数据库的智慧,其中对查询至关重要的表会出现和消失。

您在客户端的唯一希望是

  • 测试表是否存在
    您感兴趣并
  • 执行不同的 SQL 语句
    基于这些结果。

[After your edit]

听起来您需要一两个左外连接。这将为您提供 table1 和 table2 共有的所有 id,无论它们是否在 table3 中。

select t1.id, t2.id, t3.id
from table1 t1
inner join table2 t2 on (t1.id = t2.id)
left join table3 t3 on (t1.id = t3.id);

这将为您提供 table1 中的所有 id,无论它们是在 table2 还是表 3 中。

select t1.id, t2.id, t3.id
from table1 t1
left join table2 t2 on (t1.id = t2.id)
left join table3 t3 on (t1.id = t3.id);

当然,您可以使用 WHERE 子句过滤结果。

What are you doing wrong? Querying a table that doesn't exist. That's always going to raise an error.

I'm not going to address the wisdom of designing a database in which tables crucial to your queries come and go.

Your only hope on the client side is to

  • test for the existence of the tables
    you're interested in, and
  • execute different SQL statements
    based on those results.


[After your edit]

It sounds like you need either one or two left outer joins. This gives you all the ids that are common to both table1 and table2, regardless of whether they're in table3.

select t1.id, t2.id, t3.id
from table1 t1
inner join table2 t2 on (t1.id = t2.id)
left join table3 t3 on (t1.id = t3.id);

And this gives you all the ids that are in table1, regardless of whether they're in table2 or table 3.

select t1.id, t2.id, t3.id
from table1 t1
left join table2 t2 on (t1.id = t2.id)
left join table3 t3 on (t1.id = t3.id);

And, of course, you can filter the results with your WHERE clause.

伊面 2024-10-23 08:34:51

LEFT JOIN 仅在表确实存在但不包含数据时才有效。

如果您不想做类似 Catcall建议(检查表是否存在,并根据该表使用不同的SQL语句...)您必须确保该表存在于数据库中,即使它完全是空的。

在正常情况下,您应该对应用程序的数据库有一定程度的控制,因此您应该能够确保该表确实存在。

如果这确实是一个大问题(例如,您无法确定是否有人删除了该表),您可以在每次启动应用程序时进行检查:如果该表不存在,则创建该表。

LEFT JOIN only works when the table DOES exist, but contains no data.

If you don't want to have to do stuff like Catcall suggested (check if the table exists, and use different SQL statements based on that...) you have to make sure that the table exists in the database, even if it's completely empty.

Under normal circumstances, you should have a certain degree of control over your application's database, so you should be able to make sure that the table is really there.

If this is really a big problem (like, that you can't be sure if someone deleted the table) you could check this every time you start your application: create the table if it doesn't exist.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文