Click here to Skip to main content
16,017,857 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
the thing is that i don know if there is a way to get a value from a subquery over an update operation, the query that i'm trying to do looks like this, on a example code of my model :

$query = "UPDATE some_table SET value_1 = ((SELECT value_2 FROM other_table WHERE id = 2) + 1) WHERE id = 2";

$this->db->query($query);

i hope you can help me or at least let me know an alternative way to get this using the active record

PD i'm working with the CodeIgniter Framework
Posted
Updated 23-Jul-14 12:26pm
v2
Comments
Zeeshan17 30-Aug-14 12:38pm    
Sorry didn't understand what you need exactly,please be clear what you want to achieve so that i can help you.

1 solution

You can call a subQuery function where

SQL
((SELECT value_2 FROM other_table WHERE id = 2) + 1)


will be executed and be used on the same model.

Example:

Controller:
PHP
$this->load->model('example_model');
$this->example_model->mainQuery();

Model:
PHP
Class Example_model Extends CI_Model
{
function subQuery()
{
$query = "SELECT value_2 FROM other_table WHERE id = 2;
$q = $this->db->query($query);
$this->mainQuery($q->result());
} 
function mainQuery($subQuery)
{
$query = "UPDATE some_table SET value_1 = "'.$subQuery.'" WHERE id = 2";
$this->db->query($query);
}
}


If you want to use the active record(recommended) instead of queries. You can start translating your queries to active records by reading this one.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900