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
;
Related
The query I'm trying to write is
SELECT Equipment.Name, Equipment.EquipmentTag, LoanedOut.StudentNumber, LoanedOut.DueDate
FROM Equipment, LoanedOut
WHERE Equipment.EquipmentRecordID = LoanedOut.EquipmentRecordID AND LoanedOut.StudentNumber = 040828055
I can't figure out how to do it using the query builder for codeigniter, the best I have so far is
$this->db->select('Equipment.Name, Equipment.EquipmentTag, LoanedOut.StudentNumber, LoanedOut.DueDate');
$this->db->from('Equipment e, LoanedOut l');
$this->db->join('l', 'e.EquipmentRecordID = l.EquipmentRecordID')->join('l', 'l.StudentNumber', $studentNumber);
This will give you what you want//
$sql = 'SELECT e.Name,e.EquipmentTag,l.StudentNumber,l.DueDate FROM Equipment e LEFT JOIN LoanedOut l ON e.EquipmentRecordID = l.EquipmentRecordID WHERE l.StudentNumber = "040828055"'
$query = $this->db->query($sql);
I am working on a private message app using CodeIgniter.
I have 3 tables:
conversations
conversations_messages
conversations_members.
I am trying to join the tables by matching the conversations ids and fetch all the messages for the user who's logged in.
This is the standard sql query
SELECT
'conversations','conversation_id',
'conversations','conversation_subject',
MAX('conversations_messages','message_date') AS 'conversation_last_reply'
FROM 'conversations'
LEFT JOIN 'conversations_messages' ON 'conversations'.'conversation_id' = 'conversations_messages'.'conversation_id'
INNER JOIN 'conversations_members' ON 'conversations'.'conversation_id' = 'conversations_members'.'conversation_id'
WHERE 'conversations_members', 'user_id' = $sender_id
AND 'conversations_members','conversation_deleted' = 0
GROUP BY 'conversations'.'conversation_id'
ORDER BY 'conversation_last_reply' DESC";
Its a big query, hence; I'm not sure how to convert it to CodeIgniter active records or simply make it run with the framework.
Any corrections would be appreciated too.
Try below code with CodeIgniter active record.
$this->db->select('conversations, conversation_id, conversation_subject');
$this->db->select_max('conversations_messages.message_date', 'conversation_last_reply');
$this->db->from('conversations');
// Joining with conversations_messages table
$this->db->join('conversations_messages','conversations.id=conversations_messages.conversation_id','left');
$this->db->join('conversations_members','conversations.id= conversations_members.conversation_id','inner');
$this->db->where('conversations_members.user_id',$sender_id);
$this->db->where('conversations_members.conversation_deleted','0');
$this->db->group_by('conversations.conversation_id');
$this->db->order_by('conversation_last_reply');
// Get the results from db
$query = $this->db->get();
// Result array contains the records selected
$result_array = $query->result_array();
Happy coding
Naseem
$this->db->select('conversations, conversation_id, conversation_subject');
$this->db->select_max('conversations_messages.message_date', 'conversation_last_reply');
$this->db->from('conversations');
// Joining with conversations_messages table
$this->db->join('conversations_messages','conversations.id=conversations_messages.conversation_id','left');
$this->db->join('conversations_members','conversations.id= conversations_members.conversation_id','inner');
$this->db->where('conversations_members.user_id',$sender_id);
$this->db->where('conversations_members.conversation_deleted','0');
$this->db->group_by('conversations.conversation_id');
$this->db->order_by('conversation_last_reply');
// Get the results from db
$query = $this->db->get();
// Result array contains the records selected
$result_array = $query->result_array();
Im working on ecommerce platform. I have a query in normal form. i want to convert to codeigniter.
this is my query
SELECT products.product_name,products.product_id,products.short_description,pi.img,
CASE WHEN products.sp_price=0 THEN products.price WHEN products.sp_price!=0 THEN products.sp_price END as pprice FROM
(`offers_products`) JOIN `products` ON `offers_products`.`product_id` = `products`.`product_id`
LEFT JOIN (SELECT image_name as img,product_id as pid from product_images GROUP BY pid)pi
ON `products`.`product_id` = `pi`.`pid` .
How do i convert this to codeigniter query.
I tried, but getting syntax error. Please help me, im new to codeigniter.
There is no need to convert your query. And also the is no rule that you should use codeigniter query only.
you can use
$res = $this->db->query("your query here")->result();
$res will have that result() you want.
This will help you.
For more reference, check here
Just use
public function method_name()
{
$query = $this->db->query("SELECT products.product_name,products.product_id,products.short_description,pi.img, CASE WHEN products.sp_price=0 THEN products.price WHEN products.sp_price!=0 THEN products.sp_price END as pprice FROM (`offers_products`) JOIN `products` ON `offers_products`.`product_id` = `products`.`product_id` LEFT JOIN (SELECT image_name as img,product_id as pid from product_images GROUP BY pid)pi ON `products`.`product_id` = `pi`.`pid`");
$result = $query->result_array();
return $result;
}
We use result_array for pass data as Objective array
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
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();