Codeigniter Subquery - php

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();

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 would I write this query that includes joins in CodeIgniter's query builder?

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);

MySQL Query equivalent in CodeIgniter

MySQL Query is below
select PB.*, P.*, WhoCreated.*, WhoPlacedBid.* from tblprojectbid PB
Inner Join tblproject P on P.projectid = PB.projectid
Inner Join tbluser WhoCreated WhoCreated.UserID = P.WhoCreated
Inner Join tbluser WhoPlacedBid WhoPlacedBid.UserID = PB.WhoCreated
Where PB.FreelancerAwardedProjectStatusID in (4, 5)
and WhoCreated.UserID = 3
and PB.Reviewgiven = 0
Below is the query written in CodeIgnitor.
$this->_ci->db->select('PB.*, P.*, WhoCreated.*, WhoPlacedBid.*');
$this->_ci->db->from('tblprojectbid PB');
$this->_ci->db->join('tblproject P', 'P.projectid = PB.projectid');
$this->_ci->db->join('tbluser WhoCreated', 'WhoCreated.UserID = P.WhoCreated');
$this->_ci->db->join('tbluser WhoPlacedBid', 'WhoPlacedBid.UserID = PB.WhoCreated');
$this->_ci->db->or_where('PB.FreelancerAwardedProjectStatusID', 4);
$this->_ci->db->or_where('PB.FreelancerAwardedProjectStatusID', 5);
$this->_ci->db->where('WhoCreated.UserID', 5);
$this->_ci->db->where('PB.Reviewgiven', 5);
$query = $this->_ci->db->get();
Can you please help in correcting the above query to make it equivalant to MYSQL query above ?
$this->db->select() accepts an optional second parameter. If you set it to FALSE, CodeIgniter will not try to protect your field or table names with backticks. You don't need to repeat $this->_ci->db-> untill you don't want to add join or conditions with a conditional statement. Use where_in() for checking the same column name with more than one value . And you can optimize the code like this:
$this->_ci->db->select('PB.*, P.*, WhoCreated.*, WhoPlacedBid.*',FALSE)
->from('tblprojectbid PB')
->join('tblproject P', 'P.projectid = PB.projectid')
->join('tbluser WhoCreated', 'WhoCreated.UserID = P.WhoCreated')
->join('tbluser WhoPlacedBid', 'WhoPlacedBid.UserID = PB.WhoCreated')
->where_in('PB.FreelancerAwardedProjectStatusID', array(4,5))
->where(array('WhoCreated.UserID' => 5, 'PB.Reviewgiven' => 5))
->get();
Little corrections:
1) add second parameter false to select() to avoid backticks.
2) used where_in
$this->_ci->db->select('PB.*, P.*, WhoCreated.*, WhoPlacedBid.*',false);
$this->_ci->db->from('tblprojectbid PB');
$this->_ci->db->join('tblproject P', 'P.projectid = PB.projectid');
$this->_ci->db->join('tbluser WhoCreated', 'WhoCreated.UserID = P.WhoCreated');
$this->_ci->db->join('tbluser WhoPlacedBid', 'WhoPlacedBid.UserID = PB.WhoCreated');
$this->_ci->db->where_in('PB.FreelancerAwardedProjectStatusID', array(4,5));
$this->_ci->db->where('WhoCreated.UserID', 5);
$this->_ci->db->where('PB.Reviewgiven', 5);
$query = $this->_ci->db->get();
let me know if it's working.

want to set codeigniter join query

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();

How to write a LEFT JOIN in a DELETE QUERY?

Will you give me an example of a delete query that use a left join using Doctrine?
It's not possible. See that: http://trac.doctrine-project.org/ticket/2142
You have to use a subquery in the where clause: http://www.doctrine-project.org/documentation/manual/1_2/en/dql-doctrine-query-language:subqueries
Try something like that:
$q = Doctrine_Query::create()
->delete('TableB b')
->where('b.id NOT IN (SELECT b.id FROM TableB b \
INNER JOIN b.TableA a WHERE b.date > NOW() AND a.channel_id = 10)')
->execute();
Its not necessary to write the query in plain sql, you can still use the QueryBuilder for that:
$entityManager = $this->getEntityManager();
$queryBuilder = $entityManager->createQueryBuilder();
$subQueryCityDataSources = $queryBuilder->getEntityManager()->createQueryBuilder()
->select('cds')
->from(CityDataSource::class, 'cds')
->where('cds.dataSource = :dataSource')
;
$queryBuilder->delete($entityClass, 'd');
$queryBuilder->where($queryBuilder->expr()->in('d.cityDataSource', $subQueryCityDataSources->getDQL()));
$queryBuilder->setParameter('dataSource', $dataSource);
$query = $queryBuilder->getQuery();
$query->execute();
This generates this query:
DELETE
AppBundle\Entity\ConcreteData\News d
WHERE
d.cityDataSource IN
(
SELECT
cds
FROM
AppBundle\Entity\CityDataSource cds
WHERE
cds.dataSource = :dataSource
)
Important note: you have to append all the parameters in the outer query.

Categories