How to convert sql query to codeigniter query












1















Can somebody help me convert this Sql Query



SELECT *
FROM customer c
LEFT JOIN customer_order co
ON c.customer_number = co.customer_number
AND co.order_status IN ('preparing', 'prepared')
WHERE c.customer_status='unpaid'
AND c.order_status = 'unserve'
AND co.cus_ord_no IS null


into Codeigniter query just like the image below for example



enter image description here










share|improve this question

























  • Possible duplicate of How to execute my SQL query in CodeIgniter

    – sad saddest
    Nov 21 '18 at 23:57
















1















Can somebody help me convert this Sql Query



SELECT *
FROM customer c
LEFT JOIN customer_order co
ON c.customer_number = co.customer_number
AND co.order_status IN ('preparing', 'prepared')
WHERE c.customer_status='unpaid'
AND c.order_status = 'unserve'
AND co.cus_ord_no IS null


into Codeigniter query just like the image below for example



enter image description here










share|improve this question

























  • Possible duplicate of How to execute my SQL query in CodeIgniter

    – sad saddest
    Nov 21 '18 at 23:57














1












1








1








Can somebody help me convert this Sql Query



SELECT *
FROM customer c
LEFT JOIN customer_order co
ON c.customer_number = co.customer_number
AND co.order_status IN ('preparing', 'prepared')
WHERE c.customer_status='unpaid'
AND c.order_status = 'unserve'
AND co.cus_ord_no IS null


into Codeigniter query just like the image below for example



enter image description here










share|improve this question
















Can somebody help me convert this Sql Query



SELECT *
FROM customer c
LEFT JOIN customer_order co
ON c.customer_number = co.customer_number
AND co.order_status IN ('preparing', 'prepared')
WHERE c.customer_status='unpaid'
AND c.order_status = 'unserve'
AND co.cus_ord_no IS null


into Codeigniter query just like the image below for example



enter image description here







mysql codeigniter codeigniter-3






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 19:49









ropenrom24

1068




1068










asked Nov 21 '18 at 16:45









Jeremy PaulJeremy Paul

10510




10510













  • Possible duplicate of How to execute my SQL query in CodeIgniter

    – sad saddest
    Nov 21 '18 at 23:57



















  • Possible duplicate of How to execute my SQL query in CodeIgniter

    – sad saddest
    Nov 21 '18 at 23:57

















Possible duplicate of How to execute my SQL query in CodeIgniter

– sad saddest
Nov 21 '18 at 23:57





Possible duplicate of How to execute my SQL query in CodeIgniter

– sad saddest
Nov 21 '18 at 23:57












2 Answers
2






active

oldest

votes


















2














When query statements do not have clauses that need to change conditionally then using $this->db-query() is the way to go.



$sql = "SELECT * FROM customer c LEFT JOIN customer_order co 
ON c.customer_number=co.customer_number AND co.order_status IN ('preparing', 'prepared')
WHERE c.customer_status='unpaid' AND c.order_status='unserve' AND co.cus_ord_no IS null";

$query = $this->db->query($sql)->result();
echo json_encode($query);


It might be wise to include a check on the return from query() though because if it fails (returns false) then the call to result() will throw an exception. One way that can be handled is like this.



$query = $this->db->query($sql);
if($query !== FALSE)
{
echo json_encode($query->result());
return;
}

echo json_encode(); // respond with an empty array


Query Builder (QB) is a nice tool, but it is often overkill. It adds a lot of overhead to create a string that literally is passed to $db->query(). If you know the string and it doesn't need to be restructured for some reason you don't need QB.



QB is most useful when you want to make changes to your query statement conditionally. Sorting might be one possible case.



if($order === 'desc'){
$this->db->order_by('somefield','DESC');
} else {
$this->db->order_by('somefield','ASC');
}

$results = $this->db
->where('other_field', "Foo")
->get('some_table')
->result();


So if the value of $order is 'desc' the query statement would be



SELECT * FROM some_table WHERE other_field = 'Foo' ORDER BY somefield 'DESC' 


But if you insist on using Query Builder I believe this your answer



$query = $this->db
->join('customer_order co', "c.customer_number = co.customer_number AND co.order_status IN ('preparing', 'prepared')", 'left')
->where('c.customer_status','unpaid')
->where('c.order_status','unserve')
->where('co.cus_ord_no IS NULL')
->get('customer c');

//another variation on how to check that the query worked
$result = $query ? $query->result() : ;
echo json_encode($result);





share|improve this answer


























  • thank you it works

    – Jeremy Paul
    Nov 22 '18 at 10:24



















2














You can do



public function view_customers()
{
$sql = "SELECT * FROM customer c LEFT JOIN customer_order co ON c.customer_number = co.customer_number AND co.order_status IN ('preparing', 'prepared') WHERE c.customer_status='unpaid' AND c.order_status = 'unserve' AND co.cus_ord_no IS null";

return $this->db->query($sql)->result();
}


You can use row() for one output to object, or row_array() if one output but array. result() is multiple objects and result_array() is multiple arrays.



My way do usually is like this:



Controller:



public function view()
{
$this->load->model('My_Model');

$data = new stdclass;
$data->user_lists = $this->my_model->view_users(array('nationality'=>'AMERICAN'));
}


Model:



public function view_users($param = null) //no value passed
{
$condition = '1';
if (!empty($param)) { //Having this will trap if you input an array or not
foreach ($param as $key=>$val) {
$condition .= " AND {$key}='{$val}'"; //Use double quote so the data $key and $val will be read.
}
}

$sql = "SELECT * FROM users WHERE {$condition}"; //Use double quote so the data $condition will be read.
// Final out is this "SELECT * FROM users WHERE 1 AND nationality='AMERICAN'";

return $this->db->query($sql)->result();
}





share|improve this answer
























  • this method also works for me thank you!

    – Jeremy Paul
    Nov 22 '18 at 10:25











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%2f53416847%2fhow-to-convert-sql-query-to-codeigniter-query%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














When query statements do not have clauses that need to change conditionally then using $this->db-query() is the way to go.



$sql = "SELECT * FROM customer c LEFT JOIN customer_order co 
ON c.customer_number=co.customer_number AND co.order_status IN ('preparing', 'prepared')
WHERE c.customer_status='unpaid' AND c.order_status='unserve' AND co.cus_ord_no IS null";

$query = $this->db->query($sql)->result();
echo json_encode($query);


It might be wise to include a check on the return from query() though because if it fails (returns false) then the call to result() will throw an exception. One way that can be handled is like this.



$query = $this->db->query($sql);
if($query !== FALSE)
{
echo json_encode($query->result());
return;
}

echo json_encode(); // respond with an empty array


Query Builder (QB) is a nice tool, but it is often overkill. It adds a lot of overhead to create a string that literally is passed to $db->query(). If you know the string and it doesn't need to be restructured for some reason you don't need QB.



QB is most useful when you want to make changes to your query statement conditionally. Sorting might be one possible case.



if($order === 'desc'){
$this->db->order_by('somefield','DESC');
} else {
$this->db->order_by('somefield','ASC');
}

$results = $this->db
->where('other_field', "Foo")
->get('some_table')
->result();


So if the value of $order is 'desc' the query statement would be



SELECT * FROM some_table WHERE other_field = 'Foo' ORDER BY somefield 'DESC' 


But if you insist on using Query Builder I believe this your answer



$query = $this->db
->join('customer_order co', "c.customer_number = co.customer_number AND co.order_status IN ('preparing', 'prepared')", 'left')
->where('c.customer_status','unpaid')
->where('c.order_status','unserve')
->where('co.cus_ord_no IS NULL')
->get('customer c');

//another variation on how to check that the query worked
$result = $query ? $query->result() : ;
echo json_encode($result);





share|improve this answer


























  • thank you it works

    – Jeremy Paul
    Nov 22 '18 at 10:24
















2














When query statements do not have clauses that need to change conditionally then using $this->db-query() is the way to go.



$sql = "SELECT * FROM customer c LEFT JOIN customer_order co 
ON c.customer_number=co.customer_number AND co.order_status IN ('preparing', 'prepared')
WHERE c.customer_status='unpaid' AND c.order_status='unserve' AND co.cus_ord_no IS null";

$query = $this->db->query($sql)->result();
echo json_encode($query);


It might be wise to include a check on the return from query() though because if it fails (returns false) then the call to result() will throw an exception. One way that can be handled is like this.



$query = $this->db->query($sql);
if($query !== FALSE)
{
echo json_encode($query->result());
return;
}

echo json_encode(); // respond with an empty array


Query Builder (QB) is a nice tool, but it is often overkill. It adds a lot of overhead to create a string that literally is passed to $db->query(). If you know the string and it doesn't need to be restructured for some reason you don't need QB.



QB is most useful when you want to make changes to your query statement conditionally. Sorting might be one possible case.



if($order === 'desc'){
$this->db->order_by('somefield','DESC');
} else {
$this->db->order_by('somefield','ASC');
}

$results = $this->db
->where('other_field', "Foo")
->get('some_table')
->result();


So if the value of $order is 'desc' the query statement would be



SELECT * FROM some_table WHERE other_field = 'Foo' ORDER BY somefield 'DESC' 


But if you insist on using Query Builder I believe this your answer



$query = $this->db
->join('customer_order co', "c.customer_number = co.customer_number AND co.order_status IN ('preparing', 'prepared')", 'left')
->where('c.customer_status','unpaid')
->where('c.order_status','unserve')
->where('co.cus_ord_no IS NULL')
->get('customer c');

//another variation on how to check that the query worked
$result = $query ? $query->result() : ;
echo json_encode($result);





share|improve this answer


























  • thank you it works

    – Jeremy Paul
    Nov 22 '18 at 10:24














2












2








2







When query statements do not have clauses that need to change conditionally then using $this->db-query() is the way to go.



$sql = "SELECT * FROM customer c LEFT JOIN customer_order co 
ON c.customer_number=co.customer_number AND co.order_status IN ('preparing', 'prepared')
WHERE c.customer_status='unpaid' AND c.order_status='unserve' AND co.cus_ord_no IS null";

$query = $this->db->query($sql)->result();
echo json_encode($query);


It might be wise to include a check on the return from query() though because if it fails (returns false) then the call to result() will throw an exception. One way that can be handled is like this.



$query = $this->db->query($sql);
if($query !== FALSE)
{
echo json_encode($query->result());
return;
}

echo json_encode(); // respond with an empty array


Query Builder (QB) is a nice tool, but it is often overkill. It adds a lot of overhead to create a string that literally is passed to $db->query(). If you know the string and it doesn't need to be restructured for some reason you don't need QB.



QB is most useful when you want to make changes to your query statement conditionally. Sorting might be one possible case.



if($order === 'desc'){
$this->db->order_by('somefield','DESC');
} else {
$this->db->order_by('somefield','ASC');
}

$results = $this->db
->where('other_field', "Foo")
->get('some_table')
->result();


So if the value of $order is 'desc' the query statement would be



SELECT * FROM some_table WHERE other_field = 'Foo' ORDER BY somefield 'DESC' 


But if you insist on using Query Builder I believe this your answer



$query = $this->db
->join('customer_order co', "c.customer_number = co.customer_number AND co.order_status IN ('preparing', 'prepared')", 'left')
->where('c.customer_status','unpaid')
->where('c.order_status','unserve')
->where('co.cus_ord_no IS NULL')
->get('customer c');

//another variation on how to check that the query worked
$result = $query ? $query->result() : ;
echo json_encode($result);





share|improve this answer















When query statements do not have clauses that need to change conditionally then using $this->db-query() is the way to go.



$sql = "SELECT * FROM customer c LEFT JOIN customer_order co 
ON c.customer_number=co.customer_number AND co.order_status IN ('preparing', 'prepared')
WHERE c.customer_status='unpaid' AND c.order_status='unserve' AND co.cus_ord_no IS null";

$query = $this->db->query($sql)->result();
echo json_encode($query);


It might be wise to include a check on the return from query() though because if it fails (returns false) then the call to result() will throw an exception. One way that can be handled is like this.



$query = $this->db->query($sql);
if($query !== FALSE)
{
echo json_encode($query->result());
return;
}

echo json_encode(); // respond with an empty array


Query Builder (QB) is a nice tool, but it is often overkill. It adds a lot of overhead to create a string that literally is passed to $db->query(). If you know the string and it doesn't need to be restructured for some reason you don't need QB.



QB is most useful when you want to make changes to your query statement conditionally. Sorting might be one possible case.



if($order === 'desc'){
$this->db->order_by('somefield','DESC');
} else {
$this->db->order_by('somefield','ASC');
}

$results = $this->db
->where('other_field', "Foo")
->get('some_table')
->result();


So if the value of $order is 'desc' the query statement would be



SELECT * FROM some_table WHERE other_field = 'Foo' ORDER BY somefield 'DESC' 


But if you insist on using Query Builder I believe this your answer



$query = $this->db
->join('customer_order co', "c.customer_number = co.customer_number AND co.order_status IN ('preparing', 'prepared')", 'left')
->where('c.customer_status','unpaid')
->where('c.order_status','unserve')
->where('co.cus_ord_no IS NULL')
->get('customer c');

//another variation on how to check that the query worked
$result = $query ? $query->result() : ;
echo json_encode($result);






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 '18 at 20:21

























answered Nov 21 '18 at 19:58









DFriendDFriend

6,9871520




6,9871520













  • thank you it works

    – Jeremy Paul
    Nov 22 '18 at 10:24



















  • thank you it works

    – Jeremy Paul
    Nov 22 '18 at 10:24

















thank you it works

– Jeremy Paul
Nov 22 '18 at 10:24





thank you it works

– Jeremy Paul
Nov 22 '18 at 10:24













2














You can do



public function view_customers()
{
$sql = "SELECT * FROM customer c LEFT JOIN customer_order co ON c.customer_number = co.customer_number AND co.order_status IN ('preparing', 'prepared') WHERE c.customer_status='unpaid' AND c.order_status = 'unserve' AND co.cus_ord_no IS null";

return $this->db->query($sql)->result();
}


You can use row() for one output to object, or row_array() if one output but array. result() is multiple objects and result_array() is multiple arrays.



My way do usually is like this:



Controller:



public function view()
{
$this->load->model('My_Model');

$data = new stdclass;
$data->user_lists = $this->my_model->view_users(array('nationality'=>'AMERICAN'));
}


Model:



public function view_users($param = null) //no value passed
{
$condition = '1';
if (!empty($param)) { //Having this will trap if you input an array or not
foreach ($param as $key=>$val) {
$condition .= " AND {$key}='{$val}'"; //Use double quote so the data $key and $val will be read.
}
}

$sql = "SELECT * FROM users WHERE {$condition}"; //Use double quote so the data $condition will be read.
// Final out is this "SELECT * FROM users WHERE 1 AND nationality='AMERICAN'";

return $this->db->query($sql)->result();
}





share|improve this answer
























  • this method also works for me thank you!

    – Jeremy Paul
    Nov 22 '18 at 10:25
















2














You can do



public function view_customers()
{
$sql = "SELECT * FROM customer c LEFT JOIN customer_order co ON c.customer_number = co.customer_number AND co.order_status IN ('preparing', 'prepared') WHERE c.customer_status='unpaid' AND c.order_status = 'unserve' AND co.cus_ord_no IS null";

return $this->db->query($sql)->result();
}


You can use row() for one output to object, or row_array() if one output but array. result() is multiple objects and result_array() is multiple arrays.



My way do usually is like this:



Controller:



public function view()
{
$this->load->model('My_Model');

$data = new stdclass;
$data->user_lists = $this->my_model->view_users(array('nationality'=>'AMERICAN'));
}


Model:



public function view_users($param = null) //no value passed
{
$condition = '1';
if (!empty($param)) { //Having this will trap if you input an array or not
foreach ($param as $key=>$val) {
$condition .= " AND {$key}='{$val}'"; //Use double quote so the data $key and $val will be read.
}
}

$sql = "SELECT * FROM users WHERE {$condition}"; //Use double quote so the data $condition will be read.
// Final out is this "SELECT * FROM users WHERE 1 AND nationality='AMERICAN'";

return $this->db->query($sql)->result();
}





share|improve this answer
























  • this method also works for me thank you!

    – Jeremy Paul
    Nov 22 '18 at 10:25














2












2








2







You can do



public function view_customers()
{
$sql = "SELECT * FROM customer c LEFT JOIN customer_order co ON c.customer_number = co.customer_number AND co.order_status IN ('preparing', 'prepared') WHERE c.customer_status='unpaid' AND c.order_status = 'unserve' AND co.cus_ord_no IS null";

return $this->db->query($sql)->result();
}


You can use row() for one output to object, or row_array() if one output but array. result() is multiple objects and result_array() is multiple arrays.



My way do usually is like this:



Controller:



public function view()
{
$this->load->model('My_Model');

$data = new stdclass;
$data->user_lists = $this->my_model->view_users(array('nationality'=>'AMERICAN'));
}


Model:



public function view_users($param = null) //no value passed
{
$condition = '1';
if (!empty($param)) { //Having this will trap if you input an array or not
foreach ($param as $key=>$val) {
$condition .= " AND {$key}='{$val}'"; //Use double quote so the data $key and $val will be read.
}
}

$sql = "SELECT * FROM users WHERE {$condition}"; //Use double quote so the data $condition will be read.
// Final out is this "SELECT * FROM users WHERE 1 AND nationality='AMERICAN'";

return $this->db->query($sql)->result();
}





share|improve this answer













You can do



public function view_customers()
{
$sql = "SELECT * FROM customer c LEFT JOIN customer_order co ON c.customer_number = co.customer_number AND co.order_status IN ('preparing', 'prepared') WHERE c.customer_status='unpaid' AND c.order_status = 'unserve' AND co.cus_ord_no IS null";

return $this->db->query($sql)->result();
}


You can use row() for one output to object, or row_array() if one output but array. result() is multiple objects and result_array() is multiple arrays.



My way do usually is like this:



Controller:



public function view()
{
$this->load->model('My_Model');

$data = new stdclass;
$data->user_lists = $this->my_model->view_users(array('nationality'=>'AMERICAN'));
}


Model:



public function view_users($param = null) //no value passed
{
$condition = '1';
if (!empty($param)) { //Having this will trap if you input an array or not
foreach ($param as $key=>$val) {
$condition .= " AND {$key}='{$val}'"; //Use double quote so the data $key and $val will be read.
}
}

$sql = "SELECT * FROM users WHERE {$condition}"; //Use double quote so the data $condition will be read.
// Final out is this "SELECT * FROM users WHERE 1 AND nationality='AMERICAN'";

return $this->db->query($sql)->result();
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 23:51









GGwGGw

239213




239213













  • this method also works for me thank you!

    – Jeremy Paul
    Nov 22 '18 at 10:25



















  • this method also works for me thank you!

    – Jeremy Paul
    Nov 22 '18 at 10:25

















this method also works for me thank you!

– Jeremy Paul
Nov 22 '18 at 10:25





this method also works for me thank you!

– Jeremy Paul
Nov 22 '18 at 10:25


















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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53416847%2fhow-to-convert-sql-query-to-codeigniter-query%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







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith