返回两个查询的结果

时间:2014-02-11 15:25:38

标签: mysql

我有一个登录查询,我想针对两个条件运行。基本上,在查询号1中,我检查该成员是否连接到我系统中的任何合同,并且在查询2中,我检查该成员是否拥有我系统中的任何单元。

我想设置一个运行两者的查询,如果要么返回结果(m。*),那么应该返回该结果。

我还没有下面的两个问题,我想做出一个:

select m.* FROM member m
inner join unit_contract_member ucm on ucm.member_id = m.id
inner join unit_contract uc on uc.id = ucm.contract_id
inner join unit u on u.id = uc.unit_id
where m.usr = 'test@gmail.com'
and m.pwd = 'test'
and u.community_id = 1
and m.active = true
and uc.active = true;

select m.* from member m
inner join unit_owner uo on uo.member_id = m.id
inner join unit u on u.id = uo.unit_id
where m.usr = 'test@gmail.com'
and m.pwd = 'test'
and u.community_id = 1
and m.active = true;

任何人都可以帮忙吗?非常感谢。

此致

鲍勃

2 个答案:

答案 0 :(得分:0)

IF EXISTS(select m.* FROM member m
       inner join unit_contract_member ucm on ucm.member_id = m.id
       inner join unit_contract uc on uc.id = ucm.contract_id
       inner join unit u on u.id = uc.unit_id
       where m.usr = 'test@gmail.com'
       and m.pwd = 'test'
       and u.community_id = 1
       and m.active = true
       and uc.active = true)
       SELECT m.* FROM member m
       inner join unit_contract_member ucm on ucm.member_id = m.id
       inner join unit_contract uc on uc.id = ucm.contract_id
       inner join unit u on u.id = uc.unit_id
       where m.usr = 'test@gmail.com'
       and m.pwd = 'test'
       and u.community_id = 1
       and m.active = true
       and uc.active = true
ELSE
       select m.* from member m
       inner join unit_owner uo on uo.member_id = m.id
       inner join unit u on u.id = uo.unit_id
       where m.usr = 'test@gmail.com'
       and m.pwd = 'test'
       and u.community_id = 1
       and m.active = true;

答案 1 :(得分:0)

  

两个表都有相同的字段意味着你使用union所有概念它将显示所有记录。