Laravel Join on Null - mysql

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');
});

Related

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();

Laravel 5+ query builder

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

pdo add value to sql result

I use code below to get data from database
if( !empty( $books_ids ) )
{
$books_ids_in = implode(',', array_fill(0, count($books_ids), '?'));
$query = "SELECT
b.id,
b.`name`,
b.`year`,
GROUP_CONCAT(DISTINCT a.`name`) AS author_names,
GROUP_CONCAT(DISTINCT s.`name`) AS store_names
FROM
books AS b
LEFT JOIN books_authors AS b_a ON b.id = b_a.book_id
LEFT JOIN authors AS a ON a.id = b_a.author_id
LEFT JOIN books_stores AS b_s ON b.id = b_s.book_id
LEFT JOIN stores AS s ON s.id = b_s.store_id
WHERE
b.id IN (". $books_ids_in .")
GROUP BY b.id
ORDER BY b.id";
$stmt = $conn->prepare($query);
foreach ($books_ids as $k => $id) {
$stmt->bindValue(($k+1), $id);
}
$stmt->execute();
$results = $stmt->fetchAll();
}
and as I use $book id for this purpose, I would like to add some parameter in result to show that, for example, param = "book" for every row. Is there any way to do that?
Just pass it the string and alias it as a column. Though since you know the value passed in in code and display the values though code...... I'm not sure why you need this... as the value is available to you in the code when being displayed.
$query = "SELECT
b.id,
b.`name`,
b.`year`,
GROUP_CONCAT(DISTINCT a.`name`) AS author_names,
GROUP_CONCAT(DISTINCT s.`name`) AS store_names,
'". $param."' as forEveryRow
FROM
books AS b
LEFT JOIN books_authors AS b_a ON b.id = b_a.book_id
LEFT JOIN authors AS a ON a.id = b_a.author_id
LEFT JOIN books_stores AS b_s ON b.id = b_s.book_id
LEFT JOIN stores AS s ON s.id = b_s.store_id
WHERE
b.id IN (". $books_ids_in .")
GROUP BY b.id
ORDER BY b.id";

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);
}