请问mysql查询多表时一个表没有对应数据怎么办

left join查询时一个表没有数据其他的数据也不显示了

比如
select * from user u left join score s on u.sid=s.id
where u.name='a' and s.date='2021-01'

这样如果s没有2021年一月的数据就整个都查不出来了,我希望分数显示为空但是user表的数据还是要显示怎么办

你把 and s.date='2021-01' 放到 on 后面去,而不要放where后面。放where后面就是做整体条件处理了。

改动最小的

select * from user u left join score s on u.sid=s.id
where u.name='a' and ifnull(s.date,'2021-01')='2021-01'

 

试试

select * from user u left join score s on u.sid=s.id and s.date='2021-01'
where u.name='a'

这样试试

select * from 
( select * from user where name='a' ) as   u 
left join 
( select * from score where date='2021-01') as s 
on u.sid=s.id