laravel mysql查询多个where orwhere和inner join

时间:2014-11-21 14:00:33

标签: php mysql laravel

如何在Laravel中准备以下MySQL查询?

这是我使用的代码没有成功:

$user_list = DB::table('users')
->where('users.user_id' ,'=', $clientId)
->where('users.firstname', 'LIKE', '%'.$_POST['name'].'%')
->orWhere('users.lastname', 'LIKE', '%'.$_POST['name'].'%')
->orWhere('users.email', 'LIKE', '%'.$_POST['name'].'%')
->join('users_roles', 'users.id', '=', 'users_roles.user_id')                        
->where('users_roles.role_id', '=', Role::USER_PARTICIPANT)
->get();

条件必须是:

  1. users.user_id == $clientId
  2. users.firstname == '%'.$_POST['name'].'%' OR users.lastname == '%'.$_POST['name'].'%' OR users.email == '%'.$_POST['name'].'%'
  3. users_roles与跨越users.id == users_roles.user_id when users_roles.role_id == Role::USER_PARTICIPANT
  4. 的用户之间的内部联接

2 个答案:

答案 0 :(得分:22)

好的,你的问题似乎是嵌套所在的

试试这个:

$name = $_POST['name']; // I'd recommend you use Input::get('name') instead...

$user_list = DB::table('users')
    ->where('users.user_id' ,'=', $clientId)
    ->where(function($query) use ($name){
        $query->where('users.firstname', 'LIKE', '%'.$name.'%');
        $query->orWhere('users.lastname', 'LIKE', '%'.$name.'%');
        $query->orWhere('users.email', 'LIKE', '%'.$name.'%');
    })
    ->join('users_roles', 'users.id', '=', 'users_roles.user_id')
    ->where('users_roles.role_id', '=', Role::USER_PARTICIPANT)
    ->get();

答案 1 :(得分:0)

我想你想研究在join子句中使用内联函数的可能性......你的where子句可能会发生碰撞 - 而且你真的希望role_id子句在你的连接上。

http://laravel.com/docs/4.2/queries#joins

$clientId = 1;
$name = 'Phillip';
$escaped = '%' . $name . '%';

$userList = DB::table('users')
    ->where('users.user_id', $clientId)
    ->where('firstname', 'LIKE', $escaped)
    ->orWhere('lastname', 'LIKE', $escaped)
    ->orWhere('email', 'LIKE', $escaped)
    ->join('users_roles', function($join) {
        $join->on('users.user_id', '=', 'users_roles.user_id')
            ->where('users_roles.role_id', '=', Role::USER_PARTICIPANT);
    })->get();

var_dump($userList);

以上产生此查询:

select *
from `users`
inner join `users_roles` on `users`.`user_id` = `users_roles`.`user_id` and `users_roles`.`role_id` = ?
where `users`.`user_id` = ?
and `firstname` LIKE ?
or `lastname` LIKE ?
or `email` LIKE ?

鉴于以下数据库,我给了我一行:

mysql> desc users;
+-----------+------------------+------+-----+---------+----------------+
| Field     | Type             | Null | Key | Default | Extra          |
+-----------+------------------+------+-----+---------+----------------+
| user_id   | int(11) unsigned | NO   | PRI | NULL    | auto_increment |
| firstname | varchar(255)     | NO   |     | NULL    |                |
| lastname  | varchar(255)     | NO   |     | NULL    |                |
| email     | varchar(255)     | NO   |     | NULL    |                |
+-----------+------------------+------+-----+---------+----------------+
4 rows in set (0.02 sec)

mysql> desc users_roles;
+---------+------------------+------+-----+---------+-------+
| Field   | Type             | Null | Key | Default | Extra |
+---------+------------------+------+-----+---------+-------+
| user_id | int(11) unsigned | NO   |     | NULL    |       |
| role_id | int(11) unsigned | NO   |     | NULL    |       |
+---------+------------------+------+-----+---------+-------+
2 rows in set (0.03 sec)

mysql> select * from users;
+---------+-----------+------------+--------------------+
| user_id | firstname | lastname   | email              |
+---------+-----------+------------+--------------------+
|       1 | Phillip   | Harrington | philsown@gmail.com |
+---------+-----------+------------+--------------------+
1 row in set (0.00 sec)

mysql> select * from users_roles;
+---------+---------+
| user_id | role_id |
+---------+---------+
|       1 |       1 |
+---------+---------+
1 row in set (0.00 sec)