Multi column select query - codeigniter

my table field are
payid payamount paytype
01 5000 1
02 3000 1
03 2500 3
I want to get result as select cash =(select sum (payamount)where paytype=1)
online=(select sum (payamount)where paytype=2)
check=(select sum (payamount)where paytype=3)
how I can do it in codeigniter?

may be you are looking for the following
$this->db->select("SUM(payment)")
->from("TABLE_NAME")
->where("paytype", $paytype)
put the TABLE_NAME and pass the $paytype and hope it will work...

Sounds like a GROUP BY.
SELECT sum(paymount), paytype FROM mytable GROUP BY paytype;
In Codeigniter you can use
$this->db->select_sum('paymount');
$this->db->select('paytype');
$this->db->group_by('paytype');
$query = $this->db->get('mytable');

public function getincomebyyear($s,$e){
$this->db->select('income_id, income_date,income_source,income_sourceid,income_refferanceid,income_description,income_amount,(select sum(income_amount) from tbl_incomes where income_description=1) as cash,'
. '(select sum(income_amount) from tbl_incomes where income_description=2) as online,'
. '(select sum(income_amount) from tbl_incomes where income_description=3) as che');
$this->db->from('tbl_incomes');
$this->db->where('income_date between'
. ' "'. date('Y-m-d', strtotime($s)). '" and'
. ' "'. date('Y-m-d', strtotime($e)).'"');
$query_result= $this->db->get();
$result=$query_result->result();
return $result;
}

Related

How to use multiple WHERE in DB::select in Laravel

Here is the original code:
$invoice = DB::select('SELECT MAX(CAST(`invoice_number` as UNSIGNED)) as invoice_number FROM `invoices` where company_id = "' . company()->id . '" ');
return $invoice[0]->invoice_number;
The above works great, but I want to use it like this:
$invoice = DB::select('SELECT MAX(CAST(`invoice_number` as UNSIGNED)) as invoice_number FROM `invoices` where company_id = "' . company()->id . '" where depa_series = "2" ');
return $invoice[0]->invoice_number;
But this gives the following error:
Illuminate\Database\QueryException
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax;
check the manual that corresponds to your MariaDB server version for the right syntax
to use near ' where depa_series = "2"' at line 1 (SQL: SELECT MAX(CAST(`invoice_number` as UNSIGNED)) as invoice_number FROM `invoices` where company_id = "140", where depa_series = "2" )
How to use multiple WHERE clauses in DB::select?
Thank you!
The query gives a syntax error, because you use where twice.
Try to change it to this, by using the AND condition:
DB::select('SELECT MAX(CAST(`invoice_number` as UNSIGNED)) as invoice_number FROM `invoices` where company_id = "' . company()->id . '" AND depa_series = "2"');
Have a look at the selectRaw method
$invoice = DB::table('invoices')
->selectRaw('MAX(CAST(invoice_number as UNSIGNED)) as invoice_number')
->where('company_id', company()->id)
->where('depa_series', "2")
->get();
return $invoice[0]->invoice_number;
here is what you should do
$invoice =
DB::table('invoices')
->selectRaw('MAX(CAST(invoice_number as UNSIGNED)) as invoice_number')
->where(['company_id', company()->id, 'depa_series', "2"])
->get(); return $invoice[0]->invoice_number;
There will no need to using multiple where statements. You can just put all in an array.

How to order by heading and then the description

I have a table with heading and description when user searches the blog it should list first all the heading and then the description currently it is ordering by date created. And search should be case insenstive
$SQL = "(SELECT * FROM {$this->blogs} WHERE Lower(heading) LIKE '%" .strtolower($query) . "%' )
UNION
(SELECT * FROM {$this->blogs} WHERE Lower(description) LIKE '%" . strtolower($query) ."%')";
$run = $this->db->query( $SQL );
Query1
SELECT * FROM tbl_wonderblogs WHERE LOWER(heading) LIKE '%indian army amfc%' ) UNION (SELECT * FROM tbl_wonderblogs WHERE LOWER(description) LIKE '%indian army amfc%'
Query2
SELECT * FROM tbl_wonderblogs WHERE LOWER(heading) LIKE '%indian army afmc%' ) UNION (SELECT * FROM tbl_wonderblogs WHERE LOWER(description) LIKE '%indian army afmc%'
What is difference between query1 and query2 in which query 1 yeild results and query 2 does not yeild any results
The results are sorted by date created because of this code
$this->db->order_by('createdon DESC');
It asks that output is sorted by the contents of the createdon field in descending order.
I think you will find the following will make the output sort the way you want.
$this->db->order_by('description DESC, heading DESC');
There are a couple of different ways to use the order_by() method as shown in the documentation. The following will produce the same result as the previous code.
$this->db->order_by('description' 'DESC');
$this->db->order_by('heading', 'DESC');

How to set a value in join() rather a column name in Eloquent?

Here is my schemes:
user_familiarity
id, item_type, item_id, score, custom_score , ...
word
id , ...
user_familiarity stores familiarity score of any object, which can identified by (item_type, item_id)
for example:
item_type = 1001, item_id = 16 means table word and row's id is 16
Here is my code to select words with it's familiarity score:
$fam_table = UserFamiliarityModel::getTableName();
return
$query
->leftJoin($fam_table, function ($join) use($fam_table) {
$join
->on(WordModel::getTableName() . '.id' ,'=', "${fam_table}.item_id")
->on($fam_table . '.item_type' , 1001);
})
->addSelect(["${fam_table}.score", "${fam_table}.custom_score"]);
here is the sql that has been executed:
...
FROM
`word`
LEFT JOIN `user_familiarity`
ON
`word`.`id` = `user_familiarity`.`item_id`
AND `user_familiarity`.`item_type` = `1001`
and get errors :
Column not found: 1054 Unknown column '1001'
... I solve this problem by changing 'on' to 'where' :
->leftJoin($fam_table, function ($join) use($fam_table) {
$join
->on(WordModel::getTableName() . '.id' ,'=', "${fam_table}.item_id")
->where($fam_table . '.item_type' , 1001);
})
change on to where
because you have specific value

Count all the grouped rows with Eloquent

I'm trying to count all the grouped rows but have no clue how to do it, if possible, with Eloquent.
My query looks like this:
SELECT COUNT(*) FROM (SELECT date FROM `reports` WHERE project_id = X GROUP BY `date`) as t1
As of now I'm using raw database queries:
DB::select(DB::raw("SELECT COUNT(*) as total FROM (SELECT date FROM `" . (new Report)->getTable() . "` WHERE project_id = " . ((int) $this->project->id) . " GROUP BY `date`) as t1"))[0]->total
Is it possible to achieve this with Eloquent, or at least make the PHP "call" prettier?
You can use the Query Builder:
I'd do something along this (I don't think you need that subquery):
Report::where('project_id', $this->project->id)
->count(DB::raw('DISTINCT date'));

convert raw mySQL statement to laravel 4 eloquent with pagination

I'm trying to express MySQL statment in Eloquent with pagination
code:
$query = "(SELECT city_name FROM Cities WHERE city_name LIKE '%" .
$keyword . "%')
UNION
(SELECT area_name FROM Areas WHERE area_name LIKE '%" .
$keyword . "%')
UNION
(SELECT sub_location_name FROM Sub_locations WHERE sub_location_name LIKE '%" .
$keyword . "%' )";
Try This :
$cities = DB::table('Cities')->select('city_name')->where('city_name', $keyword);
$area=DB::table('Areas')->select('area_name')->where('area_name', $keyword);
$result=DB::table('Sub_locations')
->select('sub_location_name')
->where('sub_location_name', $keyword)
->union($cities)
->union($area)
->paginate(15);

Resources