Question

i am building query in kohana framework which looks like this:

DB::select('users.email')->from('users', 'roles_users')->
where('users.id', '=', 'roles_users.user_id')->
and_where('roles_users.role_id', '=', '2');

it would output:

SELECT `users`.`email` FROM `users`, `roles_users` 
WHERE `users`.`id` = 'roles_users.user_id' 
AND `roles_users`.`role_id` = '2'

so my problem is that kohana puts ' ' for me on roles_users.user_id so mysql searches for text not for actual value of the field. is there any way to solve it?

Was it helpful?

Solution

Is there a reason you are not joining the other table? Like that:

DB::select('users.email')
->from('users')
->join('roles_users', 'LEFT')
->on('users.id', '=', 'roles_users.user_id')
->where('roles_users.role_id', '=', '2');

If you don't like that, try

DB::select('users.email')
->from('users', 'roles_users')
->where('users.id', '=', DB::expr('roles_users.user_id'))
->and_where('roles_users.role_id', '=', '2');

OTHER TIPS

Have you tried with DB::expr()?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top