MySQL CASE WHEN END in Codeigniter
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
add a comment |
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
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 toCASE 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 theCASE
statement to after theEND AS
and it'll work... This is a MySQL syntax error, not attributable to CI
– Javier Larroulet
Nov 13 '18 at 2:09
add a comment |
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
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
mysql codeigniter
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 toCASE 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 theCASE
statement to after theEND AS
and it'll work... This is a MySQL syntax error, not attributable to CI
– Javier Larroulet
Nov 13 '18 at 2:09
add a comment |
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 toCASE 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 theCASE
statement to after theEND 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
add a comment |
1 Answer
1
active
oldest
votes
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
But still with the problem...
– user10369805
Nov 13 '18 at 17:34
But not solved the problem
– user10369805
Nov 14 '18 at 10:14
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
But still with the problem...
– user10369805
Nov 13 '18 at 17:34
But not solved the problem
– user10369805
Nov 14 '18 at 10:14
add a comment |
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
But still with the problem...
– user10369805
Nov 13 '18 at 17:34
But not solved the problem
– user10369805
Nov 14 '18 at 10:14
add a comment |
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
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
answered Nov 13 '18 at 2:11
Javier LarrouletJavier Larroulet
1,3201315
1,3201315
But still with the problem...
– user10369805
Nov 13 '18 at 17:34
But not solved the problem
– user10369805
Nov 14 '18 at 10:14
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 theCASE
statement to after theEND AS
and it'll work... This is a MySQL syntax error, not attributable to CI– Javier Larroulet
Nov 13 '18 at 2:09