简体   繁体   English

SQL内部联接两个表,右侧有2个匹配项

[英]SQL inner join two tables with 2 match on right

I have two tables like this: 我有两个这样的表: 在此处输入图片说明

I want to select In and Out values from log details where id=1 in log table. 我想从日志表中id=1日志详细信息中选择InOut值。 I tried this for In value: 我尝试此为In值:

SELECT Log.Time, LogDetail.Value
FROM Log where ID=1
INNER JOIN LogDetail
ON Log.ID=LogDetail.ID where Name="In";

Is this a true query? 这是一个真实的查询吗?

If you will try to run this query - you will see it can't be executed. 如果您尝试运行此查询-您将看到它无法执行。 The reason is two where statements it contains. 原因是它包含两个where语句。

Query can contain only one where statement in most DBMS (while this statement can contain multiple conditions joined together by logical operators). 在大多数DBMS中where查询只能包含一个where语句(而此语句可以包含由逻辑运算符连接在一起的多个条件)。

Also it is better to use table aliases - this makes query more readable and prevents possible ambiguities. 另外,最好使用表别名-这样可以使查询更具可读性,并避免可能出现的歧义。

So your query should look like: 因此,您的查询应如下所示:

select L.Time, LD.Value
from Log as L
    inner join LogDetail as LD on L.ID=LD.ID 
where L.ID=1 and LD.Name="In"

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

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