Reputation: 195
I am trying to turn a php mysql statement into Codeigniter syntax. The code selects all from a db but the zone difference equals to two form inputs subtracted from each other. Any help is much appreciated.
PHP Version
select * from zone_cost where zone_diff =
@(
(select zone from station_zone where station ='FORMINPUT') - (select zone from station_zone where station ='FORMINPUT')
);
Codeigniter Attempted Version
$zd = (
$this->db->select('zone');
$this->db->get_where('station_zone','station' => $this->form->input('station'));
) - ($this->db->select('zone');
$this->db->get_where('station_zone','station' => $this->form->input('station2'));
);
$this->db->select('zone_cost');
$this->db->where('zone_diff', $zd);
Upvotes: 0
Views: 1760
Reputation: 2386
you can check clean syntax of CodeIgniter as bellow.
$this->db->select('(column2 - column1) AS `remains_value`')->get_where('tablename1', array('pk' => $pk_val))->row_array();
I don't know whether this question is open or not. but the way I come to this question for subtraction of two column inside query only, it might help to future referer.
Thank you
Upvotes: 0
Reputation: 6027
I think you should do the substraction with mysql:
SELECT
station_zone.zone-sz2.zone as ZoneDiff
FROM station_zone
JOIN station_zone AS sz2 ON (sz2.station="station2")
WHERE station_zone.station="station1"
It isn't tested but I think you can use similar query. And after this you can combine your main query so you'll need only ONE query to "answer" your complete data-question.
Upvotes: 1