Execute WITH MySQL expression in a query using Laravel db builder - mysql

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.

Related

Yii 2 JSON_VALUE result as join condition in Active Record

Example we have Table A and B.
In table A we have data field with some json data.
How to be build Active Record relation using JSON_VALUE condition?
In plain sql it would look like
SELECT * FROM A
LEFT JOIN B ON B.id = JSON_VALUE(A.data, '$.paramName')
You could use a findBySql method
$sql = "SELECT * FROM A
LEFT JOIN B ON 'B.id = JSON_VALUE(A.data, " . $paramName . ")";
$model = YourModel::findBySql($sql)->all();
As far as i could find framework it self dosnot support sql function execution result as join relation. For now best way for me is to execute subqueries and use populateRelation method. ofcouse if youll find better way i would be glad to know.

Need help on select statement to be used in laravel

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

How to use DBAL query builder to build this subquery?

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.

How could I create a sub-query in cakePHP?

How could I create a sub-query in cakePHP with find method? For example:
SELECT *, (SELECT COUNT(*) FROM table2 WHERE table2.field1 = table1.id) AS count
FROM table1
WHERE table1.field1 = 'value'
!!! table2.field1 = table1.id !!!
Just for addition, you can build subquery using Cake's ORM. It will be more easy. Please read CakePHP retrieving data doc
In general you can use buildStatement() method of DataSource object. You can save all logic, including pagination etc.
You can do this one of two ways:
1. Use $this->Model->query(...)
This allows you execute SQL directly using the query you posted above but be aware that it can make your application quite brittle if the database schema were to change. Probably the fastest.(Documentation)
Example
$this->Model1->query("SELECT * FROM model;");
2. Separate the Calls
This is probably not as fast as option 1 but it does give you the ability to break your query down into a number of steps. You're also unlikely to get SQL injection which is potential risk with option 1.
Example
$model1s = $this->Model1->find
(
'all',
array
(
'conditions' => array('Model1.field1' => 'value')
)
);
foreach($model1s as $model1)
{
$model1['model2_count'] = $this->Model2->find
(
'count',
array
(
'conditions' => array
(
'Model2.field1' => $model1['id']
)
)
);
}

How can I optimize this raw SQL and perhaps implement it via CodeIgniter?

It's been a while since I've written raw SQL, I was hoping someone could help me out in optimizing this SQL query so that it works across, both, MySQL and PostgreSQL.
I would also have to implement this via CodeIgniter (2.x) using ActiveRecord, any help/advice?
SELECT *
FROM notaries, contact_notaries
WHERE notaries.id = contact_notaries.notary_id
AND WHERE ( contact_notaries.city LIKE %$criteria%
OR contact_notaries.state LIKE %$criteria
OR contact_notaries.address LIKE %$criteria%)
Thanks!
Each query can have just one WHERE clause (you don't need the second)
It's much better to put join condition into JOIN rather then WHERE.
Are you sure you really need all the columns from 2 tables (*)?
So I'd refactor it to
SELECT [field_list]
FROM notaries
INNER JOIN contact_notaries ON (notaries.id = contact_notaries.notary_id)
WHERE ( contact_notaries.city LIKE '%$criteria%'
OR contact_notaries.state LIKE '%$criteria'
OR contact_notaries.address LIKE '%$criteria%')
Using a1ex07's query:
SELECT [field_list]
FROM notaries
INNER JOIN contact_notaries ON (notaries.id = contact_notaries.notary_id)
WHERE ( contact_notaries.city LIKE '%$criteria%'
OR contact_notaries.state LIKE '%$criteria'
OR contact_notaries.address LIKE '%$criteria%')
Active record:
$this->db->select(); // Leave empty to select all fields
$this->db->join('contact_notaries', 'notaries.id = contact_notaries.notary_id', 'inner');
$this->db->like('contact_notaries.city', 'criteria');
$this->db->like('contact_notaries.state', 'criteria');
$this->db->like('contact_notaries.address', 'match');
$results = $this->db->get('notaries');
To specify a list of fields you can do $this->db->select('field_1, field_2, ...');.
http://codeigniter.com/user_guide/database/active_record.html