简体   繁体   English

在这种情况下如何获得结果?

[英]How to get the result for this condition?

Suppose, We have two tables:- Branch and User 假设,我们有两个表:- 分支用户

Branch: 科:

BranchCode  |  BranchName  | IsActive  |
----------------------------------------
   1        |    aaa       |   0       |
----------------------------------------
   2        |    bbb       |    1      |
---------------------------------------
   3        |    ccc       |     0     |

User: 用户:

UserId   |  Name  |
-------------------
  333     |  jjjj |
-------------------
  444      |  kkkk|
-------------------

We want to select result as 我们想选择结果为

MemberId
--------
2 - 333
--------
2 - 444
-------

that means, we take the branchcode for those who has isactive = 1 这意味着,我们为那些具有isactive = 1的用户采用分支代码

I am facing problem to retrieve the resultset as the table has no relation, is there a way to get the result as expected? 由于表没有关系,我在检索结果集时遇到问题,是否可以按预期方式获得结果?

Use CROSS JOIN : 使用CROSS JOIN

SELECT b.branchcode, u.UserId   
FROM brach b CROSS JOIN
     (SELECT UserId FROM User u) u
WHERE b.IsActive = 1;
SELECT CONCAT(b.branchcode,' - ', u.UserId) MemberId FROM User u,brach b WHERE b.IsActive = 1;

You might use RIGHT OUTER JOIN as : 您可以将RIGHT OUTER JOIN用作:

select concat(b.branchcode,'-',u.userid) as MemberId
  from branch b
 right outer join "user" u
    on b.isactive = 1;

MemberId
--------
2 - 333
2 - 444

ps user is a reserved keyword in MSSQL, so I've used table "user" instead. ps用户是MSSQL中的保留关键字,因此我改用了表"user"

SQL Fiddle Demo SQL小提琴演示

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

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