Yii2 : How to write distinct SQL query? - php

I want to implement following SQL queries in Yii 2 but with no success.
This should give total number of unique company names:
SELECT count(DISTINCT(company_name)) FROM clients
And this should display company_name with client code and id(PK):
SELECT (DISTINCT(company_name,client_code)) FROM clients
How to achieve this?

Try this:
$total = YourModel::find()->select('company_name')->distinct()->count();
In Search Model:
public function search($params)
{
$query = YourModel::find()->select('company_name')->distinct();
// or
$query = YourModel::find()->select(['company_name', 'client_code'])->distinct();
$query->orderBy('id desc');
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
// ...
}

Answering my own question I got following working solutions:
Got the count for unique company_name:
$my = (new yii\db\Query())
->select(['company_name',])
->from('create_client')
->distinct()
->count();
echo $my;
List of distinct company_name and client_code:
$query = new yii\db\Query();
$data = $query->select(['company_name','client_code'])
->from('create_client')
->distinct()
->all();
if ($data) {
foreach ($data as $row) {
echo 'company_name: ' . $row['company_name'] . ' client_code: ' . $row['client_code'] . '<br>';
}
}

All worked fine
return Clients::find()->count('DISTINCT(company_name)');

I hope this sample is useful for you
$names = Yii::$app->db->createCommand('SELECT count(DISTINCT(company_name)) as name FROM clients')
->queryAll();
for access the the data
foreach ($names as $name){
echo $name['name'];
}

For those who need select distinct on and other columns with aliases, the following solution will work:
$modelClass::find()
->select(new Expression(
"distinct on ('col1') col1, col2, test as col3, 'hardCodeValue' as col4"
))
Note: for PGSQL it is important what quotes you put in the query.

Related

How to pass foreach inside laravel DB::select query

I want to iterate through list of words broken down in a function processNeedle. This is working fine with ordinary php but not in laravel.
$query = $request->input('query');
$trim = new SearchTrim();
$words = $trim->ProcessNeedle($query);
$concat = "CONCAT(";
$concat.="title,";
$concat.="'')";
$sql =DB::select("SELECT DISTINCT id,title,code,abstract FROM projects WHERE 0 ";
foreach ($words as $word) $sql.=" OR $concat LIKE '%$word%'";
$sql.=" ORDER BY id DESC";
My query function well like this in php
SQL query: SELECT DISTINCT id,title,code FROM projects WHERE 0 OR CONCAT(title,'') LIKE '%intranet%' OR CONCAT(title,'') LIKE '%mailing%' ORDER BY id DESC;
How do i achieve this in Laravel Please help
Try this example . i hope it helps you .
Pass foreach inside laravel DB::select query
$items = ['condition1', 'condition2', 'condition3'];
$results = App\Model::where(function ($query) use ($items) {
foreach($items as $item) {
$query->orWhere('dbfield', 'LIKE', "%$item%");
}
})
->get();
dd($results);

Laravel table joins

I'm trying to do some table joins and having some trouble.i need to display
the customer’s complete name and title, item description, quantity ordered, for each item ordered after April 2000. i made a SQL script that works but i need to use Laravel ORM.
SELECT `first_name`,`last_name`,o.order_id,`quantity`,`description`,`date_placed`
FROM `customer` c
JOIN `order` o
ON c.`customer_id` = o. `customer_id`
JOIN `orderline` ol
ON o.`order_id` = ol. `order_id`
JOIN `item` i
ON ol.`item_id` = i. `item_id`
WHERE `date_placed` > '2000-4-00';
I created 2 models for the tables "Customer", "Order"
here is my Customer model
public function orders(){
return $this->hasMany('App\Order','customer_id');
}
here is my order model
public function orderline(){
return $this->hasMany('App\Orderline','order_id');
}
right now i am able to get some of my data but i dont feel like this is a good way to go about
$customer = Customer::all();
foreach($customer as $input){
$item = Customer::find($input->customer_id)->orders;
$name = $input->title . ' ' . $input->first_name . ' ' . $input->last_name;
$datePlaced = null;
$orderID = null;
foreach($item as $value){
$orderID = $value->order_id;
$datePlaced = $value->date_placed;
$order = Order::find($value->order_id)->orderline;
}
if anyone could point me in the right direction that would be great.
It looks like you want to get all Customers with their Orders and OrderLines?
Customer::with(['order' => function ($query) {
$query->where('date_placed', '>=', '2000-04-01')->with('orderline');
}])->get();
If you want to limit the columns on the relationships, you can...
Customer::with(['order' => function ($query) {
$query->select(/* columns */)->where('date_placed', '>=', '2000-04-01')
->with(['orderline' => function ($query) {
$query->select(/* columns here */);
}]);
}])->get();
Just make sure if you specify the columns in the relationships, that you're selecting all of the foreign keys or related columns for each relationship.

Select all tables values inside whereExist - Laravel

I have to select few other columns as well instead of just top table columns. I have the code;
$records_all = DB::table('table1')
->whereExists(function($query) use ($date) {
$query->select(DB::raw(1))
->from('table2')
->whereRaw('table2.table1_id = table1.table1_id')
->whereNotExists(function($query) use ($date) {
$query->select(DB::raw(1))
->from('table3')
->whereRaw('table2.table2_id = table3.table2_id')
->whereRaw("DATE(table3.date)='" . $date . "'");
});
})
->orderBy('url')
->get();
This gives me records for table1 when required. but how to get anything from table2 or table3 ?
I am using laravel 4.2
Thanks for any help.
change DB::raw(1) to specific column you need and separate column by comma
$records_all = DB::table(DB::raw('table1, table2, table3'))
->select('table1.*','table2.table2_id','table3.table3_id')
->whereExists(function($query) use ($date) {
$query->from('table2')
->whereRaw('table2.table1_id = table1.table1_id')
->whereNotExists(function($query) use ($date) {
$query->from('table3')
->whereRaw('table2.table2_id = table3.table2_id')
->whereRaw("DATE(table3.date)='" . $date . "'");
});
})
->orderBy('url')
->get();
to get print last executed query just after above statement
$queries = DB::getQueryLog();
$last_query = end($queries);
print_r($last_query);exit;

SQL to function in Codeigniter returns row

I'm trying to get an entire row values from a new SQL query so.
How can I make a function
Select * from table1 t1,table2 t2,table3 t3 where t1.t1_id=t2.t1_id and t3.id=t2.t3_id
to something like this :
In model page
public function getID_researcher($lastname){
$query = $this->db->get_where('researcher', array('lastname' => $lastname));
return $query->row_array();
}
I need to return one row result base on the lastname which is from table1
To clarify here, 'researcher' is the table where it gets data but what I want is the new SQL for me to get the data.
I tried this one but still error.
public function getID_researcher($lastname){
$sql = = $this->db->query('Select * from table1 t1,table2 t2,table3 t3 where t1.t1_id=t2.t1_id and t3.id=t2.t3_id');
$query = $this->db->get_where($sql, array('lastname' => $lastname));
return $query->row_array();
}
to use this query
Select * from table1 t1,table2 t2,table3 t3 where t1.t1_id=t2.t1_id and t3.id=t2.t3_id
you can try to use active record db 'join' like this:
$this->db->from('table1 t1');
$this->db->join('table2 t2', 't1.t1_id = t2.t1_id', 'left');
$this->db->join('table3 t3', 't3.id = t2.t3_id', 'left');
$this->db->where('lastname', $lastname);
$query = $this->db->get();
$c = 0;
foreach($query->result() as $q){
$result[$c]['name'] = $q->lastname;
// put move variable here
$c++;
}
return $result;
hope this help

try to perform a query with join with Zend (SE)

I have 4 tables on my DB;
merchants, pay_method, spendings ans transaction_type.
They all have an id and a title, except spend (spend have the Foreign keys of the others and other field like user_id, comment, ... ).
I try to have all the informations of all the tables for an unique user_id!
for perform it, I tried a little test that doesn't run :
public function getSpendingsForUser( $userid )
{
$mercTable = Engine_Api::_()->getDbTable('merchants', 'spendings');
$mercName = $mercTable->info('name');
$table = Engine_Api::_()->getDbTable('spendings', 'spendings');
$spendName = $table->info('name');
$select = $table->fetchAll(
$table->select()
->joinLeft($mercName, "$mercName.merchant_id = $spendName.merchant_id", NULL)
->where('user_id = ?', $userid)
->group('spending_id')
);
return $select;
}
An in the view file :
foreach($this->spendings as $s) {
var_dump($s->comment);
}
How can I make the join correctly to have all the info from the user_id ???
Thanks a lot !!!
You should define doesn't run to help us help you. But I can tell you you probably need to ->setIntegrityCheck(false);
$select = $table->fetchAll(
$table->select()
->setIntegrityCheck(false);
->joinLeft($mercName, "$mercName.merchant_id = $spendName.merchant_id", NULL)
->where('user_id = ?', $userid)
->group('spending_id')
);
Before the join.

Categories