【问题标题】:How to make where query with join如何使用 join 进行 where 查询
【发布时间】:2017-01-12 12:58:22
【问题描述】:

我有一个表格,其中有四列

  1. 身份证
  2. 用户类型
  3. 用户角色
  4. org_id

现在如果 user_type 为 1(即个体),那么 user_roleorg_id 将为空, 但如果 user_type 为 2(即组织),那么它将具有 org_id(组织的 ID)和 user_role(他在组织中的角色)。

在一个组织中存在三种类型的角色

  1. admin(只有一个人可以是管理员)。
  2. 财务部(可能不止一个)。
  3. 审批者(也可能不止一个)。

我想要什么 如果每个组织的 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


【解决方案1】:

如果它是整数,你可以加入表格,记住从“85225”中删除“”。

select * from tbl_user, tbl_user_meta where tbl_user.id = tbl_user_meta.id AND tbl_user_meta.zipcode = "85225";

【讨论】:

    【解决方案2】:
    select a.id, a.uid, b.username, a.zipcode
    from tbl_user_meta a
    inner join tbl_user b
    where a.uid = b.id ; 
    

    在codeigniter中可以这样实现。

    public function gettable(){
    $query ='select a.id, a.uid, b.username, a.zipcode
        from tbl_user_meta a
        inner join tbl_user b
        on a.uid = b.id 
        where a.zipcode = 85225';
    
       $query = $this->db->query($query);
       $result = $query->result();
       return $result; 
    }
    

    【讨论】:

      【解决方案3】:

      我想我有我的答案:

       $this->db->select('tbl_user.*,tbl_user_meta.*');
       $this->db->from('tbl_user');
       $this->db->where('user_type', 1);
       $this->db->or_where('user_role', 1);
       $this->db->where('tbl_user_meta.zipcode',85225);
       $this->db->join('tbl_user_meta','tbl_user_meta.did=tbl_user.id');
      
       $query=$this->db->get();
      
       return $query->result();
      

      【讨论】:

      • 试试我的查询是你想要的吗?
      • @MudassarSaiyed 这个有什么问题吗,因为当我在我的应用程序中测试它时,它给了我正确的数据。
      • 您的意思是您的查询提供了正确的数据而我的查询不起作用??
      • @MudassarSaiyed 不,我不是那个意思。我没有尝试您的代码,因为在发布您的答案之前,我通过查询发现了它
      猜你喜欢
      • 2012-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-16
      • 2014-03-13
      相关资源
      最近更新 更多