【发布时间】:2017-01-12 12:58:22
【问题描述】:
我有一个表格,其中有四列
- 身份证
- 用户类型
- 用户角色
- org_id
现在如果 user_type 为 1(即个体),那么 user_role 和 org_id 将为空,
但如果 user_type 为 2(即组织),那么它将具有 org_id(组织的 ID)和 user_role(他在组织中的角色)。
在一个组织中存在三种类型的角色
- admin(只有一个人可以是管理员)。
- 财务部(可能不止一个)。
- 审批者(也可能不止一个)。
我想要什么
如果每个组织的 user_type 为 2,并且所有用户的 user_type 为 1,我只想获取一个人(可能是组织的管理员)。
我的桌子
-------------------------------------
| tbl_user |
-------------------------------------
| id | user_Type | user_role | org_id|
--------------------------------------
| 1 | 1 | null | null |
| 2 | 2 | 1 | 1 |
| 3 | 2 | 2 | 1 |
| 4 | 2 | 3 | 1 |
| 5 | 2 | 3 | 1 |
| 6 | 1 | null | null |
| 7 | 2 | 1 | 2 |
| 8 | 2 | 2 | 2 |
| 9 | 2 | 3 | 2 |
| 10 | 2 | 3 | 2 |
通过查询
SELECT * FROM YourTable t
WHERE t.user_type = 1
OR t.user_role = 1
预期结果
-------------------------------------
| tbl_user |
-------------------------------------
| id | user_Type | user_role | org_id|
--------------------------------------
| 1 | 1 | null | null |
| 2 | 2 | 1 | 1 |
| 7 | 2 | 1 | 2 |
| 6 | 1 | null | null |
然后我必须将结果与 tbl_user_meta 加入(其中 zipcode = 85225)。
我的 tbl_user_meta 跟随-
-------------------------------------
| tbl_user_meta |
--------------------------------------
| id | uid | user_name | zipcode|
--------------------------------------
| 1 | 1 | abc | 85225 |
| 2 | 2 | asd | 85225 |
| 3 | 3 | asd | 85225 |
| 4 | 4 | sdf | 345678 |
| 5 | 5 | fgd | 23456 |
| 6 | 6 | rgy | 23567 |
| 7 | 7 | hyt | 12345 |
| 8 | 8 | ukl | 12345 |
| 9 | 9 | tko | 21334 |
| 10 | 10 | ert | 85225 |
我最终想要达到的就是这个记录
-------------------------------------
| tbl_user_meta |
--------------------------------------
| id | uid | user_name | zipcode|
--------------------------------------
| 1 | 1 | abc | 85225 |
P.S - 我正在使用 codeigniter 进行此操作
谢谢。
【问题讨论】:
-
到目前为止您尝试过什么?发表你的作品?你想要什么?纯 sql 还是如何使用 ci 模型构建查询?
-
你想要 user_type 是 1 和 2 的数据是对的......或者你想要 user_type 是 1 或 2 的表中的数据.. 清除这个混淆
-
@MudassarSaiyed 如果每个组织的 user_type 为 2,并且所有用户的 user_type 为 1,我只想获取一个人(可能是组织的管理员)。
-
使用我的查询我认为这是你想要的......
标签: php mysql codeigniter