SQL查询: Join two tables with where clause

标签 sql sql-server

我的 SQL 查询有问题。我得到了以下查询,这是非常基本的:

SELECT *
FROM table1
WHERE Date = CURDATE()

table1 中有一个名为 h_id 的列,还有一个 table2,其中包含 h_id 列和 name

我想连接这两个表,以便获得表 2 中当前日期的姓名。

我试过了,但似乎不起作用

SELECT t2.name, t1.field2
FROM table1 t1
INNER JOIN table2 t2 ON t1.H_ID = t2.H_ID
WHERE t1.Date = CURDATE( )

最佳答案

可能区分大小写。

table1有field2列吗?

如果不是/所以,根据你的问题,尝试这样:

SELECT t2.name
FROM table1 t1
INNER JOIN table2 t2 ON t1.h_id = t2.h_id
WHERE t1.Date = CURDATE()
<小时/>

另一种可能性是 where 子句,尝试如下:

SELECT t2.name
FROM table1 t1
INNER JOIN table2 t2 ON t1.h_id = t2.h_id
WHERE convert(varchar, t1.Date, 112) = convert(varchar, getdate(), 112)
<小时/>

最后一种可能性是 table1 和 table2 中没有任何 h_id 相等。

关于SQL查询: Join two tables with where clause,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9517915/

相关文章:

php - 如何获取 MySQL 中的所有内容之一

SQL 连接排除数据

MySQL 在安全更新模式下使用子查询进行更新

java - JDBC 使用 SELECT FOR UPDATE 锁定一行,不起作用

sql-server - SQL Server 数据库在简单恢复模式下的巨大事务日志

mysql - INNER JOIN 使用 IF 逻辑

c# - 使用 EF Core 和 ExecuteSqlCommandAsync 从存储过程中的 SELECT 语句访问结果

java - JDBC + JSF,出现错误 : Parameter index out of range (2 > number of parameters,,即 1)

sql-server - SQL Server 更新查询不使用索引

.net - 客户端服务器安装包.NET