带条件左外连接的 Mysql 子查询

标签 mysql mysql-error-1054

尝试在子查询 FROM 子句中使用外部查询的参数。

tbl1:
| id | val1 | str1 |
| 1  | 12   | sbc  |
| 2  | 22   | sds  |

tbl2:
| id | val1 | str1 |
| 1  | 1    | cp   |

尝试编写以下查询:

select * from
    tbl1 t, (select * from tbl2 where t.id = tbl2.id and tbl2.val1 = 1) tb12;

预期输出:

| id | val1 | str1 | id   | val1 | str1 |
| 1  | 12   | sbc  | 1    | 1    | cp   |
| 2  | 22   | sds  | null | null | null |

但它失败并出现错误:

/* SQL Error (1054): Unknown column 't.id' in 'where clause' */

我在这里做错了什么?

最佳答案

SELECT  *
FROM    tbl1 t
LEFT JOIN
        tbl2 t2
ON      t2.id = t.id
        AND t2.val1 = 1

关于带条件左外连接的 Mysql 子查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3186274/

相关文章:

mysql - 整个 mysql 表的唯一键?

mysql - 错误代码 : 1054. '’ 中的未知列 'where clause' '

php - 日期验证以比较 php 中的两个日期

MySQL 触发器条件逻辑有意外行为

mysql - 选择某个时间段之前和之后存在的值

mysql - 使结果排序与给定的 WHERE IN(a,b,c) 条件匹配的查询

MySQL 无法启动 - 错误 : su: warning: cannot change directory to/nonexistent: No such file or directory

SQL连接问题

MySQL:错误 1054 (42S22): 'where clause' 中的未知列

mysql - Where 子句中的未知列