select distinct clientID from Client where clientID not in (select clientID from courseDetails inner join course on coursedetails.courseID = course.courseID where coursedetails.courseID = '$courseID')
If your query is a complex one then you can use RAW query in laravel like:
$data = DB::select(DB::raw('your query'));
Reference
Note: DB::raw() is used to make arbitrary SQL commands which aren't parsed any further by the query builder. They therefore can create a vector for attack via SQL injection.
I give you a starting point:
$results = DB::table('Client')
->whereNotIn('clientID', function($query) use ($courseID) {
$query->select('clientID')
->from('courseDetails')
->join('course', 'courseDetails.courseID', '=', 'course.courseID')
->where('coursedetails.courseID', '=', $courseID);
})->get();
This should get you going. You can tweak it as you want to get your expected result.
Adding to #Mayank answer, you can run raw SQL and pass parameter like this
$result = DB::select('select distinct... where coursedetails.courseID = ? ', [$courseID]);
Related
Can't find any info on how to execute something like
WITH table AS (
SELECT colA, colB
FROM table2 INNER JOIN table1 ON table1.id = table2.colA
),
table4 AS (
SELECT moo, foo
INNER JOIN table3 ON table3.colC = table4.colD
),
......
using Laravel db query builder and the expression WITH
Does anybody have build such query and have clue how to be executed?
It's perfectly possible, I use it a lot.
For example, I have a $query and I have an array called $params (the prepared statements).
Than I do:
$connection = DB::connection('mysql');
$connection->getPdo()->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
$result = $connection->select($query, $params);
I need the PDO::ATTR_EMULATE_PREPARES since I have repeating params (e.g. multiple times :user_id in the query).
So basically, I use a raw query. It is possible to also use such a query on an eloquent model, in which case it will return models as you are used to in Laravel. But this example really shows the basic version.
I'm working on a laravel application Where I have two very similar QueryBuilder but producing different result in both conditions.
Query 1:
$ids = $this->model->leftJoin('feed', 'agents.identifier', '=', 'feed.identifier')
->whereRaw('feed.active <> agents.feed_status')
->pluck('id');
dd(count($ids)); // print 485236
Query 2:
$ids = $this->model->leftJoin('feed', 'agents.identifier', '=', 'feed.identifier')
->where('feed.active', '<>', 'agents.feed_status')
->pluck('id');
dd(count($ids)); // print 4259
I would like to know the key difference between these two QueryBuilder. Why is it producing different results, although it seems identical?
And which query returns the correct result? if I would like to find the records from agents where feed_status is not equel to feed.active.
it seem I have got the clarification. Howevere I would like to share my R&D here. Incase if anyone else got the same problem.
I have printed the raw query and get where() seems consider the third parameter as string compare instead of field compare. That's why seems the result is different.
However when we run the query with whereRaw() it's treated this as table field comparision.
Laravel Code:
$ids = $this->model->leftJoin('feed', 'agents.identifier', '=', 'feed.identifier')
->whereRaw('feed.active <> agents.feed_status')
->pluck('id');
MySql Query:
"select * from `agents` left join `feed` on `agents`.`identifier` = `feed`.`identifier` where feed.active <> agents.feed_status"
# where feed.active <> agents.feed_status
Laravel Code:
$ids = $this->model->leftJoin('feed', 'agents.identifier', '=', 'feed.identifier')
->where('feed.active', '<>', 'agents.feed_status')
->pluck('id');
MySql Query:
"select * from `agents` left join `feed` on `agents`.`identifier` = `feed`.`identifier` where `feed`.`active` <> 'agents.feed_status'"
# where feed.active <> 'agents.feed_status'
Yes, the results were meant to be different.
As where method compares a column with a literal value
->where('table.column', 'cond', 'value')
If you are looking to make comparisons in two columns without using whereRaw method; you should instead use whereColumn method
->whereColumn('table1.column1', 'cond', 'table2.column2')
My query is working fine only problem in this where clause. Please tell me what will query builder query in Laravel, equivalent to this query
WHERE (interest_request.sender_id = 6 or interest_request.to_user_id = 6)
AND interest_request.interest_status =2
and profiles.profile_id not in (6)
I am using following query builder but not working
where('to_user_id',$my_profile_id)->orwhere
('sender_id',$my_profile_id)->where('interest_status','2')
->whereNotIn('profiles.profile_id', $my_profile_id)
In your particular case, you'll have to use a nested where clause:
->where(function ($query) use ($my_profile_id) {
$query->where('to_user_id', $my_profile_id)
->orWhere('sender_id', $my_profile_id);
})
->where('interest_status', 2)
->where('profile_id', '!=', $my_profile_id)
Also you don't need to use a whereIn (or whereNotIn) query if you have only one id in the list.
i would assume it will be the following in sql:
WHERE to_user_id = $my_profile_id
OR sender_id = $my_profile_id
AND interest_status = 2
AND profiles.profile_id NOT IN ($my_profile_id)
When i use orwhere() statements i use callback functions:
where(function($query) {$query->something})
->orwhere(function($query) {$query->otherOptions})
I use Doctrine's SQL Query Builder (http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/query-builder.html)
and want to do an update case with a join. I understand that a join is not supported here. So I do this:
$query = $dbalQueryBuilder->update('s_articles_attributes', 'aa')
->set('aa.fp_pos_days', ':days')
->where('aa.articledetailsID IN (SELECT a.id FROM s_articles a WHERE a.supplierID IN (:supplierIds))')
->setParameter('days', $days_string)
->setParameter('supplierIds', $supplierIds)
->execute();
$supplierIds is string(39) "3,4,26,28,31,36,37,38,48,49,55,56,64,82"
When I run this query, nothing happens. When I do this:
$query = $dbalQueryBuilder->update('s_articles_attributes', 'aa')
->set('aa.fp_pos_days', ':days')
->where('aa.articledetailsID IN (SELECT a.id FROM s_articles a WHERE a.supplierID IN (3,4,26,28,31,36,37,38,48,49,55,56,64,82))')
->setParameter('days', $days_string)
->execute();
It works fine. Does anybody know why? And that do I do wrong? I need to use that variable...
I did solve it:
I do pass the values as an array now and critical was to declare the type as well:
->setParameter('days', $days_string)
->setParameter('supplierIds', $supplierIds, \Doctrine\DBAL\Connection::PARAM_INT_ARRAY)
try to pass an array and not a string to set parameter doctrine know how to map an "in" where clause.
explode your string
I have this query with subquery.
SELECT * FROM
(SELECT module_id FROM an_modules AS m LIMIT 20 OFFSET 0) AS m
LEFT JOIN an_module_sites AS ms ON (m.module_id = ms.module_id)
How to use DBAL to build subquery like this?
This doesn't seem to be work.
$qb->select('*')
->from(
$qb->select('module_id')
->from($this->Db->getTableName('modules'), 'm')
, 'm')
->leftJoin('m', $this->Db->getTableName('module_sites'), 'ms', 'm.module_id = ms.module_id');
$stmt = $qb->execute();
$result = $stmt->fetchAll();
I recently needed to do this to implement a pagination/sorting helper. As part of this I would take a querybuilder executed by my model and and generically count the total rows it would produce if unlimited.
To be cross platform I couldn't use rowCount and potential grouping meant I couldn't just change the fields in the select - so the best option was to remove the limits and count it as a subquery. This is what I came up with:
<?php
$totalResults = $qb->getConnection()->createQueryBuilder()
->select('COUNT(*)')
->from(
'('
.$qb
->setFirstResult(null)
->setMaxResults(null)
->resetQueryPart('orderBy')
->getSQL()
.')',
'tmp')
->execute()
->fetch(\PDO::FETCH_COLUMN);
I'm not sure how doctrine ORM handles this, but in pure DBAL at least this seems to work.