want to set codeigniter join query - php

hi i ma try to join with two table.
this i my simple sql query
$sql="SELECT windows_users_image_upload.*, `windows_users_info`.`display`
FROM `windows_users_image_upload`,`windows_users_info`
WHERE `windows_users_info`.`user_id` = `windows_users_image_upload`.`user_id`
AND ".$field."=".$value;
its work fine but i want to set this to codeingter way
and try something like this.
$this->db->select("windows_users_image_upload.*,windows_users_info.display");
$this->db->from("windows_users_info,windows_users_image_upload");
$this->db->where("windows_users_image_upload.".$field,$value);
$this->db->limit($takeTuple, $startTuple);
$this->db->join("windows_users_image_upload,windows_users_image_upload.user_id = windows_users_info.user_id");
$result = $this->db->get()->result();
but its show me error
Message: Missing argument 2 for CI_DB_active_record::join()
how can i fix this.
thanks.

This is how you use a join in CodeIgniter:
$this->db->select('*');
$this->db->from('blogs');
$this->db->join('comments', 'comments.id = blogs.id');
$query = $this->db->get();
// Produces:
// SELECT * FROM blogs
// JOIN comments ON comments.id = blogs.id1
You can see it in the User-Guide of CodeIgniter.
http://ellislab.com/codeigniter/user-guide/database/active_record.html#select

You have to remove de second table in from command:
Example:
$this->db->select('windows_users_image_upload.*,windows_users_info.display');
$this->db->from('windows_users_info');
$this->db->join("windows_users_image_upload","windows_users_image_upload.user_id = windows_users_info.user_id");
$this->db->where("windows_users_image_upload.".$field,$value);
$query = $this->db->get();

Related

Convert codeigniter query to sql query

i am little confused in wririting SQL query for joining muthiple tables.
I have perfectly write that query in codeigniter as folows
$this->db->select('*');
$this->db->from('subscription_payment');
$this->db->join('user', 'user.user_id = subscription_payment.user_id');
$this->db->join('subscription', 'subscription.subscription_id =
subscription_payment.subscription_id');
$this->db->where('subscription_payment.subscription_payment_id',$sid);
$query = $this->db->get();
$result= $query->result_array();
Help me to convert this to SQL query
Using
echo $this->db->last_query();
will produce
select * from some_table...
And this is it. Next time you can convert any query you want with easy.
select *
from subscription_payment
join user
on user.user_id = subscription_payment.user_id
join subscription
on subscription.subscription_id = subscription_payment.subscription_id
where subscription_payment.subscription_payment_id = $sid
SELECT *
FROM subscription_payment
JOIN user ON user.user_id = subscription_payment.user_id
JOIN subscription ON subscription.subscription_id = subscription_payment.subscription_id
WHERE subscription_payment.subscription_payment_id = $sid
;

How to union 2 querys in codeigniter

I have 2 querys.I want to union both querys.When am trying to add both querys not getting result.
Please find below 2 querys. Even i tried to add one single core PHP Query to in codeigniter model
But not getting result. can any one please help me on this query.
Core PHP Based query in model codeigniter-method1
$sql = "select
'Amadeus' AS SupplierName,TST_Type,TKT_Type,TKTNumber,TKT_FO_REF,BasePrice,TaxPrice,Commission1,BasePrice+TaxPrice AS TotalPrice,'issued' AS tkt_type,booking_price.CreatedBy as createdby,`TicketedDate` AS created_date,CONCAT(`a3m`.`firstname`,`a3m`.`lastname`) AS created_name,a3m.account_id,`a3m`.locationvalue AS Location_ID,`a3m`.locationdescription AS Location_Name,`a3m`.costcentervalue AS CostCenter_ID,`a3m`.costcenterdescription AS CostCenter_Name
from
data_flight.booking_price
left join `a3m_account_details` `a3m` ON `a3m`.`account_id` = data_flight.booking_price.CreatedBy
where TKTNumber!=''
union all
select
'Amadeus' AS SupplierName,'' AS TST_Type,'' AS TKT_Type,br_tkt AS TKTNumber,'' AS TKT_FO_REF,br_fare_paid_B AS BasePrice, br_tax_refund_TXT AS TaxPrice,
br_cancellation_penalty AS Commission1, br_fare_total_RFT AS TotalPrice,br_cancel_type AS tkt_type,booking_refund.br_added_by AS createdby,br_refund_date AS created_date ,CONCAT(`a3m`.`firstname`,`a3m`.`lastname`) AS created_name,a3m.account_id,`a3m`.locationvalue AS Location_ID,`a3m`.locationdescription AS Location_Name,`a3m`.costcentervalue AS CostCenter_ID,`a3m`.costcenterdescription AS CostCenter_Name
from
data_flight.booking_refund
left join `a3m_account_details` `a3m` ON `a3m`.`account_id` = data_flight.booking_refund.br_added_by
where br_tkt!=''
";
echo $this->db->query($sql);
COdeigniter based query method2
$this->db->select('"Amadeus" AS SupplierName,TST_Type,TKT_Type,TKTNumber,TKT_FO_REF,BasePrice,TaxPrice,Commission1,(BasePrice+TaxPrice) AS TotalPrice,"issued" AS tkt_type,createdby,TicketedDate as created_date,CONCAT(a3m.firstname," ",a3m.lastname) AS created_name,a3m.account_id,a3m.locationvalue AS Location_ID,a3m.locationdescription AS Location_Name,a3m.costcentervalue AS CostCenter_ID,a3m.costcenterdescription AS CostCenter_Name');
$this->db->from($this->traveldb->database.".booking_price bp");
$this->db->join('a3m_account_details a3m','a3m.account_id=createdby','left');
$this->db->where('TKTNumber !=', '');
$query1 = $this->db->get();
$this->db->select('"Amadeus" AS SupplierName,"" AS TST_Type,"" AS TKT_Type,br_tkt AS TKTNumber,"" as TKT_FO_REF,br_fare_paid_B as BasePrice,br_tax_refund_TXT as TaxPrice,br_cancellation_penalty as Commission1,br_fare_total_RFT AS TotalPrice,br_cancel_type AS tkt_type,br_added_by as createdby,br_refund_date as created_date,CONCAT(a3m.firstname," ",a3m.lastname) AS created_name,a3m.account_id,a3m.locationvalue AS Location_ID,a3m.locationdescription AS Location_Name,a3m.costcentervalue AS CostCenter_ID,a3m.costcenterdescription AS CostCenter_Name');
$this->db->from($this->traveldb->database.".booking_refund bp");
$this->db->join('a3m_account_details a3m','a3m.account_id=br_added_by','left');
$this->db->where('br_tkt !=', '');
$query2 = $this->db->get();
$query = $this->db->query($query1." UNION ".$query2);
echo $this->db->last_query();
I tried both methods in my model not getting result , tried to print query it showing erros. Please help me on this.

Codeigniter Subquery

I have this query can someone help me transfer it to codeigniter style in model.
SELECT
a.*,
(
SELECT COUNT(*) FROM `comment` c
WHERE c.comment_article_id = a.`News_News_ID`
) AS counta
FROM
`news_news` a
Update:
I try this and it work nicely but im not sure it is good practice
$sql="SELECT a.*,
( SELECT COUNT(*) FROM comment c WHERE c.comment_article_id = a.`News_News_ID` ) as counta
FROM `news_news` a";
$query = $this->db->query($sql, array('News_Cate_ID' => $cate), $start, $display);
Use join instead of sub query and according to your query you didn't need where condition while using joins
$this->db->select('a.*,COUNT(c.*) AS counta');
$this->db->from('news_news AS a');
$this->db->join('comment AS c', 'c.comment_article_id = a.News_News_ID');
$query = $this->db->get();
For more information
Try below code. This query will return each article with comment count
$this->db->select('a.*,COUNT(c.*) AS count');
$this->db->from('news_news a');
$this->db->join('comment c', 'a.News_News_ID = c.comment_article_id ','LEFT');
$this->db->group_by('a.News_News_ID')
$query = $this->db->get();

How to JOIN table with converting formate date on postgresql and Codeigniter

I'am new on postgresql, i have some query like this :
SELECT * FROM "trx_mutualfund_trade" RIGHT JOIN "trx_mutualfund_nav" ON "trx_mutualfund_trade"."mtr_trade_time"::timestamp::date = trx_mutualfund_nav.mna_efective_date ....
and i try to write CI code like this :
$this->db->select('*');
$this->db->from('mutualfund_trade');
$this->db->join('mutualfund_nav', 'mutualfund_trade.mtr_trade_time::timestamp::date = mutualfund_nav.mna_efective_date', 'right');
but that CI Code produces query like this :
SELECT * FROM "trx_mutualfund_trade" RIGHT JOIN "trx_mutualfund_nav" ON "trx_mutualfund_trade"."mtr_trade_time"::"timestamp::date" = mutualfund_nav.mna_efective_date...
So that can't run because there is an doublequote on ::"timestamp::date"
Anyone can help me to show me how to write the CI code corectly?
thanks
Use this:
$query = $this->db->query("SELECT * FROM trx_mutualfund_trade RIGHT JOIN trx_mutualfund_nav ON trx_mutualfund_trade.mtr_trade_time::timestamp::date = trx_mutualfund_nav.mna_efective_date");
$result = $query->result_array();
return $result

Convert to Codeigniter Query

How to write this query in codeigniter format?
SELECT rights_management.id, rights_management.tab_name FROM rights_management
WHERE rights_management.id NOT IN (SELECT r_m.tabid FROM r_m)
Thanks for the Help . .
You can write your sub query in CI using where clause
$this->db->select('rights_management.id, rights_management.tab_name');
$this->db->from('rights_management');
$this->db->where('`rights_management.id` NOT IN (SELECT r_m.tabid FROM `r_m`)', NULL, FALSE);
First of all you have to select r_m.tabid from table r_m and store it in array.
$this->db->select('r_m.tabid');
$this->db->from('r_m');
$query = $this->db->get();
$alb = $query->result_array();
Make it to an array
foreach($alb as $rs){
$not_need[]=$rs['tabid'];
}
After that fire the general active class using where_not_in.
$this->db->where_not_in('rights_management.id', $not_need);
$this->db->select('rights_management.id, rights_management.tab_name');
$this->db->from('rights_management');
$result= $this->db->get();
$this->db->query('SELECT rights_management.id, rights_management.tab_name FROM rights_management WHERE rights_management.id NOT IN (SELECT r_m.tabid FROM r_m)');

Categories