codeigniter, Using Case When for Where Query - php

I have this query which i want to make it work in codeigniter query form, so want to know if its possible or not.
This is the query i want to implement in Codeigniter in codeigniter's own way.
use zorkif_next;
SELECT
`osp_job_status_track`.`StatusID`,
`osp_job_status_track`.`SubStatusID`,
`osp_job_sub_status`.`CurrentStatus`
FROM
(`osp_job_details`)
LEFT JOIN
`osp_job_status_track` ON `osp_job_status_track`.`JobID` = `osp_job_details`.`JobID`
LEFT JOIN
`osp_job_status` ON `osp_job_status`.`StatusID` = `osp_job_status_track`.`StatusID`
LEFT JOIN
`osp_job_sub_status` ON `osp_job_sub_status`.`SubStatusID` = `osp_job_status_track`.`SubStatusID`
LEFT JOIN
`hr_employee_details` ON `hr_employee_details`.`EmployeeID` = `osp_job_details`.`AssignToEmployeeID`
LEFT JOIN
`osp_job_type` ON `osp_job_type`.`JobTypeID` = `osp_job_details`.`JobtypeID`
WHERE
-- `isDefault` = 1
-- AND
CASE WHEN `osp_job_status`.`StatusID` = 2
THEN `osp_job_sub_status`.`CurrentStatus` = 3
ELSE `osp_job_status`.`StatusID` >= 2
END ;
So how to implement this query in
$this->db->where($where);
???

It is not possible in CI.You need to write your own query. Please have a look on this file.You can see all the available methods there.
\system\database\DB_active_rec.php

Related

UPDATE With Multi JOINS In a Single Query

I am trying to JOIN 4 Tables in one query, it compiles okay, but returns or SET no values. Following is The QUERY i have written. A Little help Please
UPDATE tbl_venue_charges AS VC
JOIN tbl_usr_training_master AS TM ON TM.trai_master_id = VC.vnu_chrg_id
JOIN tbl_trainer_participant_expenses AS TPE ON TPE.trpex_id = VC.vnu_chrg_id
JOIN tbl_usr_training_master2 AS UTM ON UTM.trai_master2_id = VC.vnu_chrg_id
JOIN tbl_travelling_expenses AS TEX ON TEX.trv_exp_id = VC.vnu_chrg_id
SET VC.trai_master_id = TM.trai_master_id AND
VC.trpex_id = TPE.trpex_id,
VC.trai_master2_id = UTM.trai_master2_id,
VC.trv_exp_id = TEX.trv_exp_id;

Equivalent expression to MySql Left Join

I'm working on my website and I'm stuck on writing MySQL query which uses "LEFT JOIN" syntax. A simple example of my query is shown bellow:
SELECT p.surname, f.f_path as players_image
FROM player p LEFT JOIN file f ON p.id_image = f.id_file
The problem is that I'm actually using 8 different tables to get necessary data and I'm joining them after WHERE clause(I found it more easier to do that way):
SELECT p.surname, f.f_path as players_image
FROM player, file
WHERE p.id_image = f.id_file
Can I somehow modify the second queries WHERE clause to get the same result as the first query returns?
I know that Oracle DB uses syntax like WHERE p.id_image(+) = f.id_file to manage that. It tried that on MySQL data base but it gave me an error.
The original SQL query(table names are different):
SELECT sp.id_speletajs, sp.vards, sp.uzvards, ss.numurs, f.f_path as attels, k.id_komanda, p.nosaukums as pozicija
FROM speletajs sp, fails f, speletaja_statistika ss, cempionata_komanda ck, komanda k, cempionats c, sezona s, pozicija p
WHERE
sp.id_attels = f.id_fails
and sp.id_speletajs = ss.id_speletajs
and ss.id_cempionata_komanda = ck.id_cempionata_komanda
and ss.id_pozicija = p.id_pozicija
and ck.id_komanda = k.id_komanda
and ck.id_cempionats = c.id_cempionats
and c.id_sezona = s.id_sezona
and k.mana_komanda = true
and s.nosaukums = (select max(s1.nosaukums) from sezona s1)

How to query 2 tables

I need to sum the transactions in tblgl (tblgl.SUM(InMonthActual)) for a selection of cost centres (tblgl.CostCentreCode) where the following conditions are met:
tblgl.PeriodNumber = 2
tblgl.CostCentreCode = tblcostcentrehierarchy.CostCentreCode
WHERE tblcostcentrehierarchy.Level7 = "RWK312 CORPORATE"
tblgl.CostCentreCode = tblcostcentreallocations.CostCentreCode
WHERE tblcostcentreallocations.Username = "jonest"
At the moment I'm running 3 separate queries to create an array which is used in the next query.
Is there a way to do it in one (maybe using JOIN)?
I hope this query will fetch your desire data. Check and let me know if it works for you.
SELECT SUM(tb1.`InMonthActual`)
FROM `tblgl` as tb1
JOIN `tblcostcentrehierarchy` as tb2 ON tb1.`CostCetntreCode` = tb2.`CostCentreCode`
JOIN `tblcostcentreallocations` as tb3 ON tb1.`CostCetntreCode` = tb3.`CostCentreCode`
WHERE tb1.`PeriodNumber` = '2' AND tb2.`Level17` = "RWK312 CORPORATE" AND tb3.`Username` = "jonest"
Give it a try
SELECT SUM(tblgl.InMonthActual) FROM tblgl
INNER JOIN tblcostcentrehierarchy ON (tblgl.CostCentreCode = tblcostcentrehierarchy.CostCentreCode AND tblgl.PeriodNumber = 2)
INNER JOIN tblcostcentreallocations ON (tblgl.CostCentreCode = tblcostcentreallocations.CostCentreCode)
WHERE tblcostcentreallocations.Username = "jonest" AND tblcostcentrehierarchy.Level7 = "RWK312 CORPORATE"
GROUP BY tblgl.InMonthActual
Hope it works fine

Only use AND statment when if is true in mysql query

I have updated an old system and I have a query problem.
To get to business this is the direct problem:
There are new records witch are associated with an id to other records. There are also old records which are not.
Case1 : there is a INT id and when that is present the query has to use AND after an include.
Case 2 : when the value of the INT id is 0 it has to do nothing
This is the part of the query where I need to make an variable AND statement:
LEFT JOIN table v ON v.producten_id = i.producten_id AND v.t5_id = i.t5_id AND i.id = v.inkoop_id
I never used IF stamens inside a query but i am looking for something like this:
LEFT JOIN table v ON v.producten_id = i.producten_id AND v.t5_id = i.t5_id if(v.inkoop_id > 0){AND i.id = v.inkoop_id}
Try this:
LEFT JOIN table v
ON v.producten_id = i.producten_id
AND v.t5_id = i.t5_id
AND (v.inkoop_id <= 0 OR i.id = v.inkoop_id)
How about this....
LEFT JOIN table v ON v.producten_id = i.producten_id
AND v.t5_id = i.t5_id
AND ((v.inkoop_id > 0 AND i.id = v.inkoop_id) OR (v.inkoop_id = 0))

mysql trasnposing rows to columns

I need to output the following query as csv.
I can easily write php logic to transpose the rows to columns from my group_concat column
However I am keen to keep as much of the data part in the database and minimize the manipulations on the php side.
I am experimenting with the two columns below the group_concat in the query.
The problem is the abundance value also returns for life_stage column. If there is no way around this other than manipulating the group_concat key values then that's fine, I just wanted to double check. Thanks in advance
SELECT
`tr`.`tr_id_pk` as 'RecordKey',
`t`.`tax_name` as `TaxonName`,
`tr`.`tr_date` as 'Date',
`s`.`si_name` as 'SiteName',
`tr`.`tr_grid_reference` as 'GridReference',
`tr`.`tr_is_site_grid` as 'IsSiteGrid',
`r`.`rec_name` as 'Recorder',
`r`.`rec_email` as 'RecorderEmail',
`tr`.`tr_comment` as 'RecordComment',
`tr`.`tr_last_update` as 'LastUpdated',
`tr`.`tr_form_key` as 'FormKey',
`c`.`co_name` as 'County',
`vc`.`vc_name` as 'ViceCounty',
`h`.`hab_name` as 'Habitat',
GROUP_CONCAT(DISTINCT CONCAT_WS('=', `ra`.`ra_name`, `rad`.`rad_value`)) as 'RecordAttributeKeyValuePairs',
`rad`.`rad_value` AS `abundance`,
`rad`.`rad_value` AS `life_stage`
FROM
`taxon_record`as `tr`
INNER JOIN
`taxon`as `t` ON `tr`.`tax_id_fk` = `t`.`tax_id_pk`
INNER JOIN
`recorder`as `r` ON `tr`.`rec_id_fk` = `r`.`rec_id_pk`
INNER JOIN
`site`as `s` ON `tr`.`si_id_fk` = `s`.`si_id_pk`
LEFT JOIN
`county`as `c` ON `tr`.`co_id_fk` = `c`.`co_id_pk`
LEFT JOIN
`vice_county`as `vc` ON `tr`.`vc_id_fk` = `vc`.`vc_id_pk`
LEFT JOIN
`habitat`as `h` ON `tr`.`hab_id_fk` = `h`.`hab_id_pk`
LEFT JOIN
(`record_attribute_data`as `rad`
INNER JOIN `record_attribute`as `ra` ON (`rad`.`ra_id_fk` = `ra`.`ra_id_pk`)) ON (`tr`.`tr_id_pk` = `rad`.`tr_id_fk`)
WHERE
`r`.`rec_email` = 'some_email#somewhere.com'
GROUP BY `tr`.`tr_id_pk`;
What you wish to do is known as "pivoting" your data and is something for which some other RDBMS have native support, but MySQL does not (by design, as the developers feel that such manipulations belong in the presentation layer).
However, as you've hinted at, you can construct a rather horrible MySQL query to perform the pivoting operation manually (one needs to join the attributes tables to the query once for each output column):
SELECT tr.tr_id_pk, abundance, life_stage -- etc.
FROM taxon_record AS tr
LEFT JOIN (
SELECT rad.tr_id_fk, rad.rad_value AS abundance
FROM
record_attribute_data ra JOIN record_attribute rad
ON rad.ra_id_fk = ra.ra_id_pk
WHERE ra.ra_name = 'abundance'
) AS tAbundance ON tAbundance.tr_id_fk = tr.tr_id_pk
LEFT JOIN (
SELECT rad.tr_id_fk, rad.rad_value AS life_stage
FROM
record_attribute_data ra JOIN record_attribute rad
ON rad.ra_id_fk = ra.ra_id_pk
WHERE ra.ra_name = 'life_stage'
) AS tLife_Stage ON tLife_Stage.tr_id_fk = tr.tr_id_pk
-- etc.
If you choose to go down this path, you can make your life slightly easier by generating this query using either a looping construct in PHP or a prepared statement in MySQL.
Is the problem that you are fetching the same column twice under different names?:
`rad`.`rad_value` AS `abundance`,
`rad`.`rad_value` AS `life_stage`
Looks like you'll always get the same thing for abundance and life_stage that way.

Categories