我有一个查询
select c.name as companyname, u.name,u.email,u.role,a.date
from useraccount u, company c, audittrial a
where
u.status='active'
and u.companyid=c.id
and
(
u.companyid=a.companyID
and a.activity like 'User activated%'
and a.email=u.email
)
order by u.companyid desc
limit 10
因此,如果以下部分不满足,
(
u.companyid=a.companyID
and a.activity like 'User activated%'
and a.email=u.email
)
不会返回任何行..
但我想返回以下查询的结果
select c.name as companyname, u.name,u.email,u.role,a.date
from useraccount u, company c, audittrial a
where
u.status='active'
and u.companyid=c.id
order by u.companyid desc
limit 10
但是要添加它,我应该返回日期(如果可用)并且如果日期不可用则返回空值。
我该怎么做?
答案 0 :(得分:5)
多个表与where
子句相结合实际上是内连接:
select * from table1, table2 where table1.id = table2.id
与
相同select * from table1 inner join table2 on table1.id = table2.id
要使审计跟踪可选,请将其更改为left join
:
select ...
from useraccount u
join company c
on u.companyid = c.id
left join
audittrial a
on a.activity like 'User activated%'
and a.email = u.email
order by
u.companyid desc
limit 10
顺便说一句,我认为正确的拼写是审计tr ai l。
答案 1 :(得分:0)
SELECT c.name as companyname, u.name,u.email,u.role,
IF(a.date!='', a.date, null) AS audit_date
FROM "rest of query"