CI db->select() and COUNT |
[eluser]Bikun[/eluser]
Hi All! I'm trying to use COUNT inside db->select(). Here is a code I have: Code: $this->db->from('orders'); and here is an error I get: Code: Error Number: 1054 As you can see, CI thinks that `COUNT(bids`.`id)` is a name of the column. How can I make to think him another way? Thanks!
[eluser]ray73864[/eluser]
COUNT() is a group function in MySQL, so even if you got the current column error sorted out you would still receive an error from the database about mixing a group function with non-group stuff. Why not use the count_all_results() active record function after running the query?
[eluser]davidbehler[/eluser]
If you want to run that query you can this to make it work: Add a second parameter, FALSE, to your call of the select function Code: $this->db->select('COUNT(bids.id) AS offers_count, orders.id AS order_id, orders.title, auctions.id AS auction_id, auctions.price', FALSE); Add a call to the group_by with the same parameters as for the select function except the field you use the group function on and the second parameter Code: $this->db->group_by("orders.id AS order_id, orders.title, auctions.id AS auction_id, auctions.price"); See use guide for further info.
[eluser]Bikun[/eluser]
Thanks guys. The way waldmeister offered works perfectly. |
Welcome Guest, Not a member yet? Register Sign In |