1

I want to write the laravel query qhich want to has multiple select query in single query

select name,owner from project where owner in (select a.user_name from users a left join users b on a.manager=b.firstname where a.user_name='sridhar.ps' or a.manager like '%sridhar p%' or b.manager like '%sridhar p%') and customer_code='OTH_0071'

The above query list the outpu what I want exactly.I want to change the query in laravel

2 Answers 2

1

with the help of whereRaw you can do that like this

DB::table('project')->where('customer_code','OTH_0071')->whereRaw("owner in (select a.user_name from users a left join users b on a.manager=b.firstname where a.user_name='sridhar.ps' or a.manager like '%sridhar p%' or b.manager like '%sridhar p%')")->select('name','owner')->get();

for using variable

DB::table('project')->where('customer_code','OTH_0071')->whereRaw("owner in (select a.user_name from users a left join users b on a.manager=b.firstname where a.user_name='sridhar.ps' or a.manager like '%".$manager."%' or b.manager like '%sridhar p%')")->select('name','owner')->get();

for more info vist laravel $manager

hope it will help you!

Sign up to request clarification or add additional context in comments.

4 Comments

in raw query I want to use variable
you can just concatenate it
$project_code=DB::table('project')->where('customer_code',$customer_name)->whereRaw("owner in (select a.user_name from users a left join users b on a.manager=b.firstname where a.user_name=".$user_name." or a.manager =".$first_name." or b.manager =".$first_name.")")->pluck('name','code')->all(); i have tried.but not working
1

I might first rewrite your query using a series of joins:

SELECT
    p.name,
    p.owner
FROM project p
INNER JOIN
(
    SELECT a.user_name
    FROM users a
    LEFT JOIN users b
        ON a.manager = b.firstname
    WHERE
        a.user_name = 'sridhar.ps'   OR
        a.manager LIKE '%sridhar p%' OR
        b.manager LIKE '%sridhar p%'
) t
    ON p.owner = t.user_name
WHERE
    p.customer_code = 'OTH_0071';

Then build the Laravel query using a raw subquery to represent the table aliased as t above:

$subquery  = "(SELECT a.user_name FROM users a LEFT JOIN users b ";
$subquery .= "ON a.manager = b.firstname ";
$subquery .= "WHERE a.user_name = 'sridhar.ps' OR a.manager LIKE '%sridhar p%' ";
$subquery .= "OR b.manager LIKE '%sridhar p%') AS t";
DB:table('project)
    ->select('name', 'owner')
    ->join(DB::raw($subquery), 'p.owner', '=', 't.user_name')
    ->where('customer_code','OTH_0071')
    ->get();

This join approach might be more performant than what you currently have. In any case, you can test this answer, compare it against the answer by @Gaurav, and then use whichever works the best.

3 Comments

@user3386779 Sorry I had omitted that. I updated my answer. Just make the call to where as you normally would; you don't need a raw query for this.
I have tried to select name and code but not working
but not working ... can you be more precise than this? What is the error message? Can you try running my raw MySQL query as well?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.