Laravel 5+ query builder - mysql

Can any one help me to get laravel join query out put like:
LEFT JOIN table2 ON table2.field1 IN (table1.field1 , table1.field2)
This means, I want to join table2 with any one of the value (that is, table1.field1 or table1.field2)

LEFT JOIN table2 ON table2.field1 = table1.field2 OR table2.field1 = table1.field1
Using Query Builder
$records = DB::table('table1')
->leftJoin('table2', function ($join) {
$join->on('table2.field1', '=', 'table1.field1')
->orOn('table2.field1' , '=', 'table1.field2');
})
->get();
Read more here: Laravel Joins

LEFT JOIN table2 ON table2.field1 IN (table1.field1 , table1.field2) this is incorrect. The correct join format is:
LEFT JOIN table2 ON table2.field1 ON table1.field1 = table1.field2

you can join two column like that . does this help!
$records = DB::table('table1 as t1')
->leftJoin('table2 as t2', function ($join) {
$join->on('t2.field1', '=', 't1.field1');
$join->orOn('t2.field1', '=', 't1.field2');
})->get();
see here Advanced Join Clauses

Related

Laravel Join on Null

As the title says, how do I join on a null condition in Laravel? Take the following query:
SELECT * FROM TABLE1 a
INNER JOIN TABLE2 b
ON a.col1 = b.col1
OR a.col2 IS NULL
I've tried this but not the result I wanted:
$query = Table1::join('table2', function($join){
$join->on('table1.col1', '=', 'table2.col1');
$join->on('table1.col2', ????????);
});
Thanks.
you can use where inside JoinClause class, see advanced join clause:
$query = Table1::join('table2', function($join){
$join->on('table1.col1', '=', 'table2.col1')
->orWhereNull('table1.col2');
});

"Join expression not supported" for more than two ON conditions with an INNER JOIN

I have been struggling with various syntax errors on a query in MS Access that works fine in TSQL.
For example, I learned that MS Access requires parentheses when there are more than one left/inner joins, and also if there is more than one ON condition for any of those joins, thanks to this post and this post.
However, I am now stuck with another "Join expression not supported" error, when I have three ON conditions for an INNER JOIN:
SELECT *
FROM ((
(Table1
INNER JOIN Table2 ON Table2.Col2 = Table1.Col2)
LEFT JOIN Table3
ON (Table1.id = Table3.fid
and Table3.Col2 > SomeNumber)
)
LEFT JOIN Table4
ON (Table4.Col2 = Table3.Col2
and Table4.Col4 = 'SomeValue'
and Table4.fid = Table1.x_id)
)
The above gets the "Join expression not supported" error, but if I remove any one of the three conditions in the last LEFT JOIN ON, the SQL was fine, like this:
SELECT *
FROM ((
(Table1
INNER JOIN Table2 ON Table2.Col2 = Table1.Col2)
LEFT JOIN Table3
ON (Table1.id = Table3.fid
and Table3.Col2 > SomeNumber)
)
LEFT JOIN Table4
ON (Table4.Col2 = Table3.Col2
and Table4.fid = Table1.x_id)
)
Any two of the three conditions was accepted in the above. I have even tried adding additional parentheses like this but it didn't work either:
LEFT JOIN Table4
ON ((Table4.Col2 = Table3.Col2
and Table4.Col4 = 'SomeValue')
and Table4.fid = Table1.x_id)
)
Please help - thanks!

Trying to convert a query to eloquent

I have the following SQL:
SELECT arv.*
FROM article_reference_versions arv
INNER JOIN (SELECT `order`,
Max(`revision`) AS max_revision
FROM article_reference_versions
WHERE `file` = '12338-230180-1-CE.doc'
GROUP BY `file`,
`order`) AS b
ON arv.order = b.order
AND arv.revision = b.max_revision
WHERE arv.file = '12338-230180-1-CE.doc'
I need to convert this to Eloquent, so that I can properly access the data in object form. I tried doing it as such,
$s = Models\EloArticleReferenceVersion::select(
'SELECT arv.*
FROM article_reference_versions arv
INNER JOIN (
SELECT `order`, max(`revision`) as max_revision
FROM article_reference_versions
WHERE file = ? group by `file`, `order`) AS b
ON
arv.order = b.order AND arv.revision = b.max_revision
WHERE arv.file = ?',
[
'12338-230180-1-CE.doc',
'12338-230180-1-CE.doc'
])->get();
dd($s);
But I'm running into a plethora of issues, one after another. I figured it'd be easier to just convert this into an eloquent query, looking for some help with this.
DB Query to Query using Eloquent.
$query = EloArticleReferenceVersion::query()
->join(DB::raw('( SELECT `order`,Max(`revision`) AS max_revision FROM article_reference_versions WHERE `file` = '12338-230180-1-CE.doc' GROUP BY `file`, `order`) as sub_table'), function($join) {
$join->on('sub_table.order', '=', 'article_reference_versions.order');
$join->on('sub_table.max_revision ', '=', 'article_reference_versions.revision');
})
->where('article_reference_versions.file', '=', '12338-230180-1-CE.doc' )
->get();
Not Tested

Converting MySQL query to Laravel

How I can convert this MySQL query to a Laravel query?
select *
from marques
where id in (select marque_id from products
where category_id = 'valeur1' or category_id in (select id from categories
where parent_id = 'Valeur1'))
I think your current query is equivalent to the following:
SELECT *
FROM marques m
LEFT JOIN products p
ON m.id = p.marque_id
LEFT JOIN categories c
ON p.category_id = c.id AND c.parent_id = 'Valeur1'
WHERE
p.category_id = 'valeur1' OR
c.id IS NOT NULL
Here is a rough guess at what your Laravel code might look like:
$res = DB::table('marques')
->join('products', 'marques.id', '=', 'products.marque_id')
->join("categories", function($join) {
$join->on('products.category_id', '=', 'categories.id')
->on('categories.parent_id', '=', 'Valeur1')
})
->whereNotNull('categories.id')
->orWhere('products.category_id', '=', 'valeur1')
->select('*')
->get();

Select statement into another select To Eloquant

How can I translate a SQL query like this to Eloquent or QueryBuilder :
SELECT * FROM studies
where studies.id in(SELECT study_id FROM (
SELECT max(studies.end_date), studies.id as study_id
from workers inner join resumes on workers.id=resumes.worker_id
inner join studies on resumes.id=studies.resume_id where
resumes.title="main" group by workers.id) as SQ2
Or globally
How can we make select from other select statement with eloquant for exemple:
SELECT a.id from (SELECT * FROM A INNER JOIN B ON a.id=b.id where a.id > 10) as SUBQ1
I think your current query doesn't get study_id with max end_date in all cases, you can try this one:
SELECT * FROM studies
where studies.end_date in (
SELECT max(studies.end_date)
from workers inner join resumes on workers.id=resumes.worker_id
inner join studies on resumes.id=studies.resume_id where
resumes.title="main" group by workers.id)
and if you want implement that with laravel Eloquant you can do it like this:
$result = DB::table('studies')
->select("*")
->whereIn('end_date', function($query){
$query->selectRaw("max(studies.end_date) as max_date")
->from('workers')
->join('resumes', 'workers.id', '=', 'resumes.worker_id')
->join('studies', 'resumes.id', '=', 'studies.resume_id')
->where('resumes.title', '=', 'main')
->groupBy('workers.id');
})
->get();
foreach($result as $row) {
print_r($row);
}