codeigniter query issue - mysql

I have this query:
$this->db->select('COUNT(tran_ID) AS count, CASE WHEN MONTH(days)>=4 THEN concat(YEAR(days), "-", YEAR(days)+1) ELSE concat(YEAR(days)-1, "-", YEAR(days)) END AS format_date,product_class AS saleCol');
I get syntax error on
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHEN MONTH(days)>=4 THEN concat(YEAR(days), `"-"`, YEAR(days)+1) ELSE concat(YEA' at line 1
SELECT COUNT(tran_ID) AS count, `CASE` WHEN MONTH(days)>=4 THEN concat(YEAR(days), `"-"`, YEAR(days)+1) ELSE concat(YEAR(days)-1, `"-"`, YEAR(days)) END AS format_date, `product_class` AS saleCol FROM (`transactions`) WHERE `trans_type` = 'E' AND `product_class` != '0' GROUP BY `format_date`, `product_class`

Try this query, added FALSE as the second parameter in select statement
$this->db->select(
'COUNT(tran_ID) AS count,
CASE WHEN MONTH(days)>=4 THEN concat(YEAR(days), "-", YEAR(days)+1)
ELSE concat(YEAR(days)-1, "-", YEAR(days))
END AS format_date,
product_class AS saleCol',FALSE);

Related

Trying to perform divison using MySQL subqueries but getting syntax error

I am trying to get the value from MySQL subqueries but I am getting 'Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1'
The code is:
UPDATE national_avg_tariff SET Value= (CASE WHEN ((SELECT * FROM((SELECT Value
/ ((Select Sum_CPP_MlnRs from ppp_fy2020_2030_annex4.FY_CPP_exclKE_MlnRs where FiscalYear=2020) + (Select UoSC_MoF_exclKE_MlnRs from ppp_fy2020_2030_annex4.FY_UoSC_MoF_exclKE_MlnRs where FiscalYear=2020))/ (SELECT Sum(Sum_of_AvgMW) FROM ppp_fy2020_2030_annex4.AVG_OF_FY_MAX_DEMAND where FiscalYear=2020 GROUP BY FiscalYear)FROM tariff_true_cost.national_avg_tariff WHERE Sector='Residential' AND Category='Up to 50 Units' AND Parameter= 'Fixed Charge(Rs/kW/M)' AND FiscalYear=2020)temptbl8))>0)
THEN
(SELECT * FROM((SELECT Value
/ ((Select Sum_CPP_MlnRs from ppp_fy2020_2030_annex4.FY_CPP_exclKE_MlnRs where FiscalYear=2020) + (Select UoSC_MoF_exclKE_MlnRs from ppp_fy2020_2030_annex4.FY_UoSC_MoF_exclKE_MlnRs where FiscalYear=2020))/ (SELECT Sum(Sum_of_AvgMW) FROM ppp_fy2020_2030_annex4.AVG_OF_FY_MAX_DEMAND where FiscalYear=2020 GROUP BY FiscalYear)FROM tariff_true_cost.national_avg_tariff WHERE Sector='Residential' AND Category='Up to 50 Units' AND Parameter= 'Fixed Charge(Rs/kW/M)' AND FiscalYear=2020)temptbl8))
ELSE NULL END) where FiscalYear=2020 AND Parameter='Fixed Charge(%)';
The code doesn't show any red underline or pre-execution error something, the error only appears once I execute it.
Any help in rectifying syntax error of this piece of code would be appreciable. Thanks

How to implement "SHOW COLUMN FROM (SELECT Result) WHERE Column_name = 'column_name'"?

Good Evning My Brothers/Sisters.
I want to check whether a column is exist from select result.
I have tried some effort like below but instead it gives me an error.
SHOW COLUMNS
FROM (SELECT
hasil.skpd, hasil.kode_skpd, SUM(hasil.sudah_isi) AS sudah_isi, SUM(hasil.belum_isi) AS belum_isi
FROM
(
SELECT
a.id_pegawai, c.skpd, c.kode_skpd, a.id_satuan_organisasi, a.nama_pegawai, a.nip,
CASE
WHEN i.id_pegawai IS NULL THEN 0
ELSE 1
END AS sudah_isi,
CASE
WHEN i.id_pegawai IS NULL THEN 1
ELSE 0
END AS belum_isi
FROM
tbl_pegawai a
LEFT JOIN ref_skpd c ON a.id_satuan_organisasi = c.id_skpd
LEFT JOIN tbl_bapertarum i ON (a.id_pegawai = i.id_pegawai AND YEAR(i.tgl_lapor)='2015')
GROUP BY
a.id_pegawai
) hasil
WHERE
1 AND hasil.kode_skpd LIKE 'Badan Kepegawaian Daerah%'
GROUP BY
hasil.id_satuan_organisasi
ORDER BY
hasil.kode_skpd) WHERE Field = "kode_skpd"
This is the full error result:
#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 '(SELECT
hasil.skpd, hasil.kode_skpd, SUM(hasil.sudah_isi) AS s' at line 2
How do i to find the existence of a column or columns from SELECT result ?
Thanks in advance Brothers/Sisters.

Mysql case statement not returning appropriate message after case evaluation

I am selecting engineStatus field from car table, the query looks like so :-
select engineStatus from car LIMIT 1
car table column can have values 1 or 0.
Assuming engineStatus is set to 1, the above query will return 1.
I want to return ON if engineStatus = 1 or return OFF when engineStatus = 0
I've tried below query, but SQL throws an Error
SELECT ( case engineStatus = 1 then SET engineStatus = 'ON' else SET status = 'OFF') FROM `car` WHERE 1
Mysql Says
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'then SET engineStatus = 'ON' else SET engineStatus = 'OFF') FROM `car` WHERE 1 LI' at line 1
How can i achieve this in mysql ?
You miss WHEN and there's no need SET here
The proper syntax is :
SELECT
( CASE
WHEN engineStatus = 1 THEN 'ON'
ELSE 'OFF'
END
) AS status
FROM `car`
WHERE 1
It should be:
SELECT ( case WHEN engineStatus = 1 then 'ON' else
'OFF' END) FROM `car`;

mysql if then statement not working?

Am I blind, or what is wrong with my query?
select
STRCMP( message, 'LogMessage') = 1
from
LogEntries;
works fine. However
select
IF STRCMP( message, 'LogMessage') = 1 THEN 'bla' END IF
from
LogEntries;
returns:
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'STRCMP( message, 'LogMessage') = 1 THEN 'bla' END IF from
LogEntries' at line 2
What is wrong with this statement?
You might want to use CASE WHEN:
SELECT
CASE WHEN STRCMP( message, 'LogMessage') = 1 THEN 'bla' END AS your_column
FROM
LogEntries;
when the condition is true it will return 'bla' otherwise, since there's no else part, it will return NULL.

returning a yes or no value based on 2 matching fields

I had a Previous Question about setting a value to yes or no based on fields from 2 table matching. I am trying to have the query check the generalsource.firstinventor and if it matches inventor.inventorfull insert "Yes" or else insert "No"
The result was the following code:
UPDATE i
SET LeadInventor =
CASE
WHEN fi.FirstInventorName IS NULL THEN 'No'
ELSE 'Yes'
END
FROM Inventor i
LEFT JOIN FirstInventor fi
ON i.InventorFirst + ' ' + i.InventorLast = fi.FirstInventorName
Which worked fantastically, until I tried to put into MySQL and I can not figure out whats wrong with the syntax. How do I fix it?
Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM Inventor i LEFT JOIN FirstInventor fi ON i.InventorFirst + ' ' + i.' at line 7
CONCAT string as
CONCAT( i.InventorFirst, ' ', i.InventorLast )
UPDATE: Also, there seems to be syntactic problem in your update statement, it should be as follows
UPDATE
Inventor i
LEFT JOIN FirstInventor fi ON CONCAT( i.InventorFirst, ' ', i.InventorLast ) = fi.FirstInventorName
SET LeadInventor =
CASE
WHEN fi.FirstInventorName IS NULL THEN 'No'
ELSE 'Yes'
END
More info: http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat