简体   繁体   English

如果满足条件,是否可以选择一个表并加入另一个表?

[英]is it possible to select a table and join another table if the condition was met?

is it possible to select a table and join another table if the condition was met? 如果满足条件,是否可以选择一个表并加入另一个表?

like select * from table if (condition, join a table, nothing to do) 就像select * from table if (condition, join a table, nothing to do)

action log table 动作日志表

id  |  user_id |  action                            |   log_time       
    |          | 0=login,1=logout,2=register_client |   
 1  |   001    |             0                      |   2014-07-20 03:10:20 
 2  |   001    |             3                      |   2014-07-20 03:10:34
 3  |   001    |             3                      |   2014-07-20 03:11:58

registration log table 注册日志表

id | user_id | client_id |  date_reg
1  |  001    | 186       |   2014-07-20 03:10:34
2  |  001    | 187       |   2014-07-20 03:11:58

the result that i got is redundant. 我得到的结果是多余的。 maybe there's is something wrong with my table. 也许我的桌子有问题。 i need to display the first table joined with the 2nd table in which the result should 3 rows only showing 3 activites which is login and registers 2 times based on the 1st table. 我需要显示与第二个表连接的第一个表,其中结果应3行仅显示3个活动,这是登录并基于第一个表注册2次。

select t1.id, t1.type,t2.details 
from table1 t1 
left outer join table2 t2
where 
t1.id=t2.id and 
t1.type=t2.type

据我所知,这是最好的方法:SELECT table1.id,table1。[用户ID],table1.type,table2.details from table1左联接table2 ON table1.type = table2.type WHERE(条件)

You are looking for left join. 您正在寻找左联接。 You don't need a condition. 您不需要条件。 It will give null when there is no corresponding line in table b. 当表b中没有对应的行时,它将为null。

select t1.*, t2.details
from table1 t1
left join table2 t2 on t1.id = t2.id and t1.type = t2.type

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

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