MySQL CASE WHEN END in Codeigniter










0














Currently I am working on Codeigniter projected related to stores management. In model I used the following function to get the purchase & issues summary for the view through controller.



Function



function issueDetailReport($id,$start,$end)
$this->db->select('*, store_update_stock_details.item,
CASE
WHEN store_update_stock.order_status = "purchase" THEN tbl_supplier.supplier_name
WHEN store_update_stock.order_status = "issue" THEN store_officer.officer_name
END AS supplier');
$this->db->from('store_update_stock');
$this->db->join('store_update_stock_details','store_update_stock.update_stock_id=store_update_stock_details.update_stock_id');
$this->db->join('store_officer','store_update_stock.supplier=store_officer.officer_id');
$this->db->join('tbl_supplier','store_update_stock.supplier=tbl_supplier.supplier_id');
$this->db->join('store_item','store_update_stock_details.item=store_item.item_id');
$this->db->where("store_update_stock.status='1' and store_item.item_id=$id");
//$this->db->where('store_update_stock.update_stock_id in (select update_stock_id from store_update_stock) ');
if($start!=NULL && $end!=NULL)
$this->db->where("store_update_stock.billed_date BETWEEN '$start' AND '$end'");
$this->db->order_by('store_update_stock.purchased_date','DESC');
$q=$this->db->get();
if($q->num_rows()>0)
return $q->result();

return false;



02) All are working fine. But the case constructor fires the following error



A Database Error Occurred
Error Number: 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 '.order_status = "issue" THEN store_officer.officer_name END AS supplier FR' at line 1



03) I tried to solve the issue. But did't get the desired output. I can't find the error & what may be wrong. Can anyone help me ?



Supplier Table



+-------------+---------------+
| supplier_id | supplier_name |
+-------------+---------------+
| 500 | ABC |
| 501 | DEF |
| 502 | GHI |
| 503 | JKL |
+-------------+---------------+


officer Table



+------------+--------------+
| officer_id | officer_name |
+------------+--------------+
| 1000 | Danial |
| 1001 | Jhon |
| 1002 | William |
| 1003 | Patrick |
| 1004 | Salman |
+------------+--------------+


Output



+------+--------------------------+------------+------------+--------------+
| item | supplier / officer_name | start | end | order_status |
+------+--------------------------+------------+------------+--------------+
| A4 | ABC | 2018-11-01 | 2018-11-01 | purchase |
| A5 | DEF | 2018-11-01 | 2018-11-01 | purchase |
| A3 | Danial | 2018-11-02 | 2018-11-02 | issue |
| B5 | Jhon | 2018-11-05 | 2018-11-05 | issue |
+------+--------------------------+------------+------------+--------------+









share|improve this question























  • Can you output the generated query? Your function seem to be ok.
    – DanB
    Nov 12 '18 at 18:21






  • 1




    Try to change case statement to CASE store_update_stock.order_status WHEN "purchase" THEN tbl_supplier.supplier_name WHEN "issue" THEN store_officer.officer_name END AS supplier
    – DanB
    Nov 12 '18 at 18:22










  • @Daniel. My output as same as the edit
    – user10369805
    Nov 13 '18 at 1:10







  • 1




    Move the * from before the CASE statement to after the END AS and it'll work... This is a MySQL syntax error, not attributable to CI
    – Javier Larroulet
    Nov 13 '18 at 2:09















0














Currently I am working on Codeigniter projected related to stores management. In model I used the following function to get the purchase & issues summary for the view through controller.



Function



function issueDetailReport($id,$start,$end)
$this->db->select('*, store_update_stock_details.item,
CASE
WHEN store_update_stock.order_status = "purchase" THEN tbl_supplier.supplier_name
WHEN store_update_stock.order_status = "issue" THEN store_officer.officer_name
END AS supplier');
$this->db->from('store_update_stock');
$this->db->join('store_update_stock_details','store_update_stock.update_stock_id=store_update_stock_details.update_stock_id');
$this->db->join('store_officer','store_update_stock.supplier=store_officer.officer_id');
$this->db->join('tbl_supplier','store_update_stock.supplier=tbl_supplier.supplier_id');
$this->db->join('store_item','store_update_stock_details.item=store_item.item_id');
$this->db->where("store_update_stock.status='1' and store_item.item_id=$id");
//$this->db->where('store_update_stock.update_stock_id in (select update_stock_id from store_update_stock) ');
if($start!=NULL && $end!=NULL)
$this->db->where("store_update_stock.billed_date BETWEEN '$start' AND '$end'");
$this->db->order_by('store_update_stock.purchased_date','DESC');
$q=$this->db->get();
if($q->num_rows()>0)
return $q->result();

return false;



02) All are working fine. But the case constructor fires the following error



A Database Error Occurred
Error Number: 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 '.order_status = "issue" THEN store_officer.officer_name END AS supplier FR' at line 1



03) I tried to solve the issue. But did't get the desired output. I can't find the error & what may be wrong. Can anyone help me ?



Supplier Table



+-------------+---------------+
| supplier_id | supplier_name |
+-------------+---------------+
| 500 | ABC |
| 501 | DEF |
| 502 | GHI |
| 503 | JKL |
+-------------+---------------+


officer Table



+------------+--------------+
| officer_id | officer_name |
+------------+--------------+
| 1000 | Danial |
| 1001 | Jhon |
| 1002 | William |
| 1003 | Patrick |
| 1004 | Salman |
+------------+--------------+


Output



+------+--------------------------+------------+------------+--------------+
| item | supplier / officer_name | start | end | order_status |
+------+--------------------------+------------+------------+--------------+
| A4 | ABC | 2018-11-01 | 2018-11-01 | purchase |
| A5 | DEF | 2018-11-01 | 2018-11-01 | purchase |
| A3 | Danial | 2018-11-02 | 2018-11-02 | issue |
| B5 | Jhon | 2018-11-05 | 2018-11-05 | issue |
+------+--------------------------+------------+------------+--------------+









share|improve this question























  • Can you output the generated query? Your function seem to be ok.
    – DanB
    Nov 12 '18 at 18:21






  • 1




    Try to change case statement to CASE store_update_stock.order_status WHEN "purchase" THEN tbl_supplier.supplier_name WHEN "issue" THEN store_officer.officer_name END AS supplier
    – DanB
    Nov 12 '18 at 18:22










  • @Daniel. My output as same as the edit
    – user10369805
    Nov 13 '18 at 1:10







  • 1




    Move the * from before the CASE statement to after the END AS and it'll work... This is a MySQL syntax error, not attributable to CI
    – Javier Larroulet
    Nov 13 '18 at 2:09













0












0








0







Currently I am working on Codeigniter projected related to stores management. In model I used the following function to get the purchase & issues summary for the view through controller.



Function



function issueDetailReport($id,$start,$end)
$this->db->select('*, store_update_stock_details.item,
CASE
WHEN store_update_stock.order_status = "purchase" THEN tbl_supplier.supplier_name
WHEN store_update_stock.order_status = "issue" THEN store_officer.officer_name
END AS supplier');
$this->db->from('store_update_stock');
$this->db->join('store_update_stock_details','store_update_stock.update_stock_id=store_update_stock_details.update_stock_id');
$this->db->join('store_officer','store_update_stock.supplier=store_officer.officer_id');
$this->db->join('tbl_supplier','store_update_stock.supplier=tbl_supplier.supplier_id');
$this->db->join('store_item','store_update_stock_details.item=store_item.item_id');
$this->db->where("store_update_stock.status='1' and store_item.item_id=$id");
//$this->db->where('store_update_stock.update_stock_id in (select update_stock_id from store_update_stock) ');
if($start!=NULL && $end!=NULL)
$this->db->where("store_update_stock.billed_date BETWEEN '$start' AND '$end'");
$this->db->order_by('store_update_stock.purchased_date','DESC');
$q=$this->db->get();
if($q->num_rows()>0)
return $q->result();

return false;



02) All are working fine. But the case constructor fires the following error



A Database Error Occurred
Error Number: 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 '.order_status = "issue" THEN store_officer.officer_name END AS supplier FR' at line 1



03) I tried to solve the issue. But did't get the desired output. I can't find the error & what may be wrong. Can anyone help me ?



Supplier Table



+-------------+---------------+
| supplier_id | supplier_name |
+-------------+---------------+
| 500 | ABC |
| 501 | DEF |
| 502 | GHI |
| 503 | JKL |
+-------------+---------------+


officer Table



+------------+--------------+
| officer_id | officer_name |
+------------+--------------+
| 1000 | Danial |
| 1001 | Jhon |
| 1002 | William |
| 1003 | Patrick |
| 1004 | Salman |
+------------+--------------+


Output



+------+--------------------------+------------+------------+--------------+
| item | supplier / officer_name | start | end | order_status |
+------+--------------------------+------------+------------+--------------+
| A4 | ABC | 2018-11-01 | 2018-11-01 | purchase |
| A5 | DEF | 2018-11-01 | 2018-11-01 | purchase |
| A3 | Danial | 2018-11-02 | 2018-11-02 | issue |
| B5 | Jhon | 2018-11-05 | 2018-11-05 | issue |
+------+--------------------------+------------+------------+--------------+









share|improve this question















Currently I am working on Codeigniter projected related to stores management. In model I used the following function to get the purchase & issues summary for the view through controller.



Function



function issueDetailReport($id,$start,$end)
$this->db->select('*, store_update_stock_details.item,
CASE
WHEN store_update_stock.order_status = "purchase" THEN tbl_supplier.supplier_name
WHEN store_update_stock.order_status = "issue" THEN store_officer.officer_name
END AS supplier');
$this->db->from('store_update_stock');
$this->db->join('store_update_stock_details','store_update_stock.update_stock_id=store_update_stock_details.update_stock_id');
$this->db->join('store_officer','store_update_stock.supplier=store_officer.officer_id');
$this->db->join('tbl_supplier','store_update_stock.supplier=tbl_supplier.supplier_id');
$this->db->join('store_item','store_update_stock_details.item=store_item.item_id');
$this->db->where("store_update_stock.status='1' and store_item.item_id=$id");
//$this->db->where('store_update_stock.update_stock_id in (select update_stock_id from store_update_stock) ');
if($start!=NULL && $end!=NULL)
$this->db->where("store_update_stock.billed_date BETWEEN '$start' AND '$end'");
$this->db->order_by('store_update_stock.purchased_date','DESC');
$q=$this->db->get();
if($q->num_rows()>0)
return $q->result();

return false;



02) All are working fine. But the case constructor fires the following error



A Database Error Occurred
Error Number: 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 '.order_status = "issue" THEN store_officer.officer_name END AS supplier FR' at line 1



03) I tried to solve the issue. But did't get the desired output. I can't find the error & what may be wrong. Can anyone help me ?



Supplier Table



+-------------+---------------+
| supplier_id | supplier_name |
+-------------+---------------+
| 500 | ABC |
| 501 | DEF |
| 502 | GHI |
| 503 | JKL |
+-------------+---------------+


officer Table



+------------+--------------+
| officer_id | officer_name |
+------------+--------------+
| 1000 | Danial |
| 1001 | Jhon |
| 1002 | William |
| 1003 | Patrick |
| 1004 | Salman |
+------------+--------------+


Output



+------+--------------------------+------------+------------+--------------+
| item | supplier / officer_name | start | end | order_status |
+------+--------------------------+------------+------------+--------------+
| A4 | ABC | 2018-11-01 | 2018-11-01 | purchase |
| A5 | DEF | 2018-11-01 | 2018-11-01 | purchase |
| A3 | Danial | 2018-11-02 | 2018-11-02 | issue |
| B5 | Jhon | 2018-11-05 | 2018-11-05 | issue |
+------+--------------------------+------------+------------+--------------+






mysql codeigniter






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 1:17







user10369805

















asked Nov 12 '18 at 17:55









user10369805user10369805

86




86











  • Can you output the generated query? Your function seem to be ok.
    – DanB
    Nov 12 '18 at 18:21






  • 1




    Try to change case statement to CASE store_update_stock.order_status WHEN "purchase" THEN tbl_supplier.supplier_name WHEN "issue" THEN store_officer.officer_name END AS supplier
    – DanB
    Nov 12 '18 at 18:22










  • @Daniel. My output as same as the edit
    – user10369805
    Nov 13 '18 at 1:10







  • 1




    Move the * from before the CASE statement to after the END AS and it'll work... This is a MySQL syntax error, not attributable to CI
    – Javier Larroulet
    Nov 13 '18 at 2:09
















  • Can you output the generated query? Your function seem to be ok.
    – DanB
    Nov 12 '18 at 18:21






  • 1




    Try to change case statement to CASE store_update_stock.order_status WHEN "purchase" THEN tbl_supplier.supplier_name WHEN "issue" THEN store_officer.officer_name END AS supplier
    – DanB
    Nov 12 '18 at 18:22










  • @Daniel. My output as same as the edit
    – user10369805
    Nov 13 '18 at 1:10







  • 1




    Move the * from before the CASE statement to after the END AS and it'll work... This is a MySQL syntax error, not attributable to CI
    – Javier Larroulet
    Nov 13 '18 at 2:09















Can you output the generated query? Your function seem to be ok.
– DanB
Nov 12 '18 at 18:21




Can you output the generated query? Your function seem to be ok.
– DanB
Nov 12 '18 at 18:21




1




1




Try to change case statement to CASE store_update_stock.order_status WHEN "purchase" THEN tbl_supplier.supplier_name WHEN "issue" THEN store_officer.officer_name END AS supplier
– DanB
Nov 12 '18 at 18:22




Try to change case statement to CASE store_update_stock.order_status WHEN "purchase" THEN tbl_supplier.supplier_name WHEN "issue" THEN store_officer.officer_name END AS supplier
– DanB
Nov 12 '18 at 18:22












@Daniel. My output as same as the edit
– user10369805
Nov 13 '18 at 1:10





@Daniel. My output as same as the edit
– user10369805
Nov 13 '18 at 1:10





1




1




Move the * from before the CASE statement to after the END AS and it'll work... This is a MySQL syntax error, not attributable to CI
– Javier Larroulet
Nov 13 '18 at 2:09




Move the * from before the CASE statement to after the END AS and it'll work... This is a MySQL syntax error, not attributable to CI
– Javier Larroulet
Nov 13 '18 at 2:09












1 Answer
1






active

oldest

votes


















0














Try changing:



$this->db->select('*, store_update_stock_details.item,
CASE
WHEN store_update_stock.order_status = "purchase" THEN tbl_supplier.supplier_name
WHEN store_update_stock.order_status = "issue" THEN store_officer.officer_name
END AS supplier');


To:



$this->db->select('store_update_stock_details.item,
CASE
WHEN store_update_stock.order_status = "purchase" THEN tbl_supplier.supplier_name
WHEN store_update_stock.order_status = "issue" THEN store_officer.officer_name
END AS supplier, *');


MySQL doesn't like stuff in the select statements after *'s and many times it'll refuse to work






share|improve this answer




















  • But still with the problem...
    – user10369805
    Nov 13 '18 at 17:34











  • But not solved the problem
    – user10369805
    Nov 14 '18 at 10:14










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53267594%2fmysql-case-when-end-in-codeigniter%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Try changing:



$this->db->select('*, store_update_stock_details.item,
CASE
WHEN store_update_stock.order_status = "purchase" THEN tbl_supplier.supplier_name
WHEN store_update_stock.order_status = "issue" THEN store_officer.officer_name
END AS supplier');


To:



$this->db->select('store_update_stock_details.item,
CASE
WHEN store_update_stock.order_status = "purchase" THEN tbl_supplier.supplier_name
WHEN store_update_stock.order_status = "issue" THEN store_officer.officer_name
END AS supplier, *');


MySQL doesn't like stuff in the select statements after *'s and many times it'll refuse to work






share|improve this answer




















  • But still with the problem...
    – user10369805
    Nov 13 '18 at 17:34











  • But not solved the problem
    – user10369805
    Nov 14 '18 at 10:14















0














Try changing:



$this->db->select('*, store_update_stock_details.item,
CASE
WHEN store_update_stock.order_status = "purchase" THEN tbl_supplier.supplier_name
WHEN store_update_stock.order_status = "issue" THEN store_officer.officer_name
END AS supplier');


To:



$this->db->select('store_update_stock_details.item,
CASE
WHEN store_update_stock.order_status = "purchase" THEN tbl_supplier.supplier_name
WHEN store_update_stock.order_status = "issue" THEN store_officer.officer_name
END AS supplier, *');


MySQL doesn't like stuff in the select statements after *'s and many times it'll refuse to work






share|improve this answer




















  • But still with the problem...
    – user10369805
    Nov 13 '18 at 17:34











  • But not solved the problem
    – user10369805
    Nov 14 '18 at 10:14













0












0








0






Try changing:



$this->db->select('*, store_update_stock_details.item,
CASE
WHEN store_update_stock.order_status = "purchase" THEN tbl_supplier.supplier_name
WHEN store_update_stock.order_status = "issue" THEN store_officer.officer_name
END AS supplier');


To:



$this->db->select('store_update_stock_details.item,
CASE
WHEN store_update_stock.order_status = "purchase" THEN tbl_supplier.supplier_name
WHEN store_update_stock.order_status = "issue" THEN store_officer.officer_name
END AS supplier, *');


MySQL doesn't like stuff in the select statements after *'s and many times it'll refuse to work






share|improve this answer












Try changing:



$this->db->select('*, store_update_stock_details.item,
CASE
WHEN store_update_stock.order_status = "purchase" THEN tbl_supplier.supplier_name
WHEN store_update_stock.order_status = "issue" THEN store_officer.officer_name
END AS supplier');


To:



$this->db->select('store_update_stock_details.item,
CASE
WHEN store_update_stock.order_status = "purchase" THEN tbl_supplier.supplier_name
WHEN store_update_stock.order_status = "issue" THEN store_officer.officer_name
END AS supplier, *');


MySQL doesn't like stuff in the select statements after *'s and many times it'll refuse to work







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 2:11









Javier LarrouletJavier Larroulet

1,3321315




1,3321315











  • But still with the problem...
    – user10369805
    Nov 13 '18 at 17:34











  • But not solved the problem
    – user10369805
    Nov 14 '18 at 10:14
















  • But still with the problem...
    – user10369805
    Nov 13 '18 at 17:34











  • But not solved the problem
    – user10369805
    Nov 14 '18 at 10:14















But still with the problem...
– user10369805
Nov 13 '18 at 17:34





But still with the problem...
– user10369805
Nov 13 '18 at 17:34













But not solved the problem
– user10369805
Nov 14 '18 at 10:14




But not solved the problem
– user10369805
Nov 14 '18 at 10:14

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53267594%2fmysql-case-when-end-in-codeigniter%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto