selectdistinct
u.id as id ,
u.name as name ,
p.ip_addr ip
from t_user u
leftjoin t_login_log p on p.user_id = u.id
2、左表1条数据,右表2条数据,就会出现重复的数据,DISTINCT无法解决,使用子查询来实现
selectdistinct
u.id as id ,
u.name as name ,
p.ip_addr ip ,
t2.age
from t_user u
leftjoin t_login_log p on p.user_id = u.id
leftjoin(selectdistinct t.age , t.user_id from t_age t) t2 on u.id = t2.user_id
-- where IFNULL(t2.age,'0') != "0" and IFNULL(p.ip_addr,'0') != "0"
3、左表1条数据,右表2条数据,就会出现重复的数据,使用group_by关键字来实现
select
u.name,
u.id ,
p.ip_addr
FROM t_user u
leftjoin t_login_log p on p.user_id = u.id
WHERE u.id='1'GROUPBY u.id;
4、俄罗斯套娃
select t1.*, t2.age from(selectdistinct
u.id as id ,
u.name as name ,
p.ip_addr ip
from t_user u
leftjoin t_login_log p on p.user_id = u.id
-- where IFNULL(p.ip_addr,'0') != "0" ) t1
leftjoin t_age t2 on t1.id = t2.user_id
-- where IFNULL(t2.age,'0') != "0" ;
5、连表查询–where
select*from t_system_version_range where system_version_range_id in(select system_version_range_id from t_release_policy trp where id in(select release_policy_id from t_configuration_items_v2 tciv where general_configuration_id in(select id from t_general_configuration_v2 tgcv where modifier !='zhouxi1001')))
6、连表查询后删除 --JOIN
DELETE tar
FROM t_system_version_rang tar
JOIN t_release_policy trp ON tar.system_version_range_id = trp.system_version_range_id
JOIN t_configuration_items_v2 tciv ON trp.id = tciv.release_policy_id
JOIN t_general_configuration_v2 tgcv ON tciv.general_configuration_id = tgcv.id
WHERE tgcv.modifier ='${userID}'
7、连表查询后删除查询以为的数据–where
select*from t_system_version_range where system_version_range_id in(select system_version_range_id from t_release_policy trp where id in(select release_policy_id from t_configuration_items_v2 tciv where general_configuration_id in(select id from t_general_configuration_v2 tgcv where modifier !='zhouxi1001')))