MySQL join two table query in codeigniter - php

I have two table, limit and transaction
$wherehis= array('status'=>'Yes','company_id' =>$compnayid);
$this->db->select('type,tra.amount,tra.qty,limit_id');
$this->db->where($wherehis);
$this->db->limit(100);
$this->db->order_by('tra.tra_id','DESC');
$this->db->join('limit_history as sell', 'tra.sell_limit_id = sell.limit_id','left');
$this->db->join('limit_history as buy', 'tra.buy_limit_id = buy.limit_id','left');
$data['Histroy']= $this->db->get("tbltrancation as tra")->result_array();
but get only sell order....please help me thanx
get the buy and sell both data

Try this,
$this->db->limit(100);
$this->db->order_by('tra.tra_id','DESC');
$this->db->join('limit_history as sell', '(tra.sell_limit_id = sell.limit_id AND tra.buy_limit_id = buy.limit_id)','left');
$data['Histroy']= $this->db->get("tbltrancation as tra")->result_array();

When there are identical column names you need to give them an alias to differentiate them:
$this->db
->select('tra.*, sell.amount as sell_amount, sell.qty as sell_qty, buy.amount as buy_amount, buy.qty as buy_qty')
->from('tbltrancation as tra')
->join('limit_history as sell', 'tra.sell_limit_id = sell.limit_id', 'left')
->join('limit_history as buy', 'tra.buy_limit_id = buy.limit_id', 'left')
->order_by('tra.tra_id', 'desc')
->limit(100);
$data['Histroy'] = $this->db->get()->result_array();

Related

Order by not working with Group by in codeigniter

$loginuserID = $this->session->userdata("loginuserID");
$this->db->select('notice_x_user.*,notice.*,classes.*,sub_courses.*,,notice.status as studentStatus,notice.date as noticeDate');
$this->db->from('notice_x_user');
$this->db->join('notice','notice_x_user.noticeID = notice.noticeID', 'LEFT');
$this->db->join('classes', 'classes.ClassesID = notice.classesID', 'LEFT');
$this->db->join('sub_courses', 'sub_courses.sub_coursesID = notice.sub_coursesID', 'LEFT');
$wheres = "(notice_x_user.userID = '".$loginuserID."' and notice_x_user.usertype = 'Support') or notice.userID = '".$loginuserID."'";
$this->db->where($wheres);
$this->db->order_by('notice.noticeID', 'DESC');
$this->db->group_by('notice.noticeID');
I am join four table where I get correct data but the problem is that order by is not working while using group by. So, How can I do this? Please help me.
Thank You
use select_max for getting the max noticeID
$this->db->select('notice_x_user.*,notice.*,classes.*,sub_courses.*,,notice.status as studentStatus,notice.date as noticeDate');
$this->db->select_max('notice.noticeID' , 'noticeID');
$this->db->from('notice_x_user');
$this->db->join('notice','notice_x_user.noticeID = notice.noticeID', 'LEFT');
$this->db->join('classes', 'classes.ClassesID = notice.classesID', 'LEFT');
$this->db->join('sub_courses', 'sub_courses.sub_coursesID = notice.sub_coursesID', 'LEFT');
$wheres = "(notice_x_user.userID = '".$loginuserID."' and notice_x_user.usertype = 'Support') or notice.userID = '".$loginuserID."'";
$this->db->where($wheres);
$this->db->order_by('noticeID', 'DESC');
$this->db->group_by('notice.noticeID');
Grouping statements must be used before ordering statements, otherwise you'll get a MySQL error.
Just put the statements in the correct order:
$this->db->group_by('notice.noticeID');
$this->db->order_by('notice.noticeID', 'DESC');
and it'll work as expected

Codeigniter : Join 2 tables based on where condition

I have to select data from two tables for APIs.
There are two tables garage_services, and garages. I receive garage_sevices id.
Garage_services table is like this
id | garage_id | service _id
Now from this table I select row based on service_id from here I have to select garage_id and get details about garage from garage table
$garages = $this->db
->select('*')
->from('garage_services')
->join('garage', 'garage_services.id=garage.id')
->where($where)
->get();
Above is the query I came up with, I don't know if its correct. as I am not sure about $where as well.
Please help me with this
Here I have written the query for your solution.
$garages = $this->db->from('garage_services as gs, garages as g')
->where('g.id', $garage_service_id, FALSE)
->get();
Here $garage_service_id is the variable that passed in function argument as you have written you in your question.
Try this
->select('*')
->from('garage_services as t1')
->join('garage as t2', 't1.garage_id = t2.id')
->where('t1.id', $service _id)
->get();
As per the detail is given in the question your query look correct and for the where condition:
$this->db->select('*');
$this->db->from('table1');
$this->db->join('table2', 'table1.col1 = table2.col1');
$this->db->where('table1.col1', 2);
$query = $this->db->get();

codeigniter join count another table

$query=$this->db->select('*')
->from('activation')
->join('products','products.id_pro = activation.id_pro')
->join('user','user.id = products.user_p')
->order_by('id_key','DESC')
->get();
return $query->result();
I have these code where I join result from 3 tables abd that works good. What I need more is counted rows from 4.table. That table is called license, and I need to count how many rows are with id_key (primary key for table activation). How to add that in my code?
Try selecting license column as count
$query=$this->db->select('*, COUNT(license.id_key ) as license_count')
->from('activation')
->join('products','products.id_pro = activation.id_pro')
->join('user','user.id = products.user_p')
->join('license','license.id_key = table.column?')
->order_by('id_key','DESC')
->get();
access the count within the object like: $query->result()->license_count;
$query=$this->db->select('BaseTbl.*', 'Products.*', 'User.*', COUNT(License.id_key) as license_count)
->from('activation as BaseTbl')
->join('products as Products','Products.id_pro = BaseTbl.id_pro')
->join('user as User','User.id = Products.user_p')
->join('license as License','License.id_key = BaseTbl.id_key')
->order_by('BaseTbl.id_key','DESC')
->group_by('BaseTbl.id_key')
->get();

Query for getting values from multiple tables

I have a table video with fields videoid, genre(int-foreign key), language(int-foreign key) etc. I want to get the value of genre from genre table and language from movie_languages table.
The structure of tables are given below:
video
genre
movie_languages
How can I join these 3 tables to get the language and genre related to each videos in video table. Also when user didn't select genre/language in the form, value 0 will be inserted to the table. Will this affect the query. I am using codeingiter and I tried with the following query and is not working.
$this->db->select('video.*,movie_languages.language,genre.genre');
$this->db->join('genre', 'video.genre = genre.id');
$this->db->join('movie_languages', 'video.language = movie_languages.id');
$query = $this->db->get();
Please help me.
Thanks in advance.
It will be, you need a left join in this case
Try this
$query = $this->db
->select('v.*,ml.language,g.genre')
->from('video as v')
->join('movie_languages AS ml', 'v.language = ml.id', 'left outer')
->join('genre AS g', 'v.genre = g.id', 'left outer')
->get();
try this
$this->db->select('video.*,movie_languages.language,genre.genre')
->from('video')
->join('genre', 'video.genre = genre.id')
->join('movie_languages', 'video.language = movie_languages.id');
$query = $this->db->get();

Codeigniter Active Record / MySQL Join Query - How to Return Results if one of the Table Rows is Not Present

I have the following query:
$this->db
->select('SQL_CALC_FOUND_ROWS null as rows
,table1.*
,table2.*
,table3.*', FALSE)
->from('table1')
->where('table1.column1', $user_id)
->join('table2', 'table2.column2 = table1.column2')
->join('table3', 'table3.column2 = table1.column2')
->group_by('table1.column2')
->order_by('table1.column2', 'DESC');
$query = $this->db->get();
The problem is, there may not be a row in table 3 and if there is not, I would still like to return a result with the remainder of the query data. Please could someone advise how to achieve this?
you should do a left join on table3
Use left join and also use group_by to get exact records:
$this->db->join('login_ranknames AS t4', 't4.id = t1.rank', 'left');
$this->db->group_by('t4.id');

Categories