Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select, count and where using codeigniter and mysql

I have two table as follow:

- tblSaler

    SalerID  |  SalerName | 
    ----------------------|
    1        |  sothorn   |
    ----------------------|
    2        |  Daly      |  
    ----------------------|
    3        |  Lyhong    |
    ----------------------|
    4        | Chantra    |
    ----------------------|

- tblProduct

ProductID  | Product  | SalerID |
--------------------------------|
1          | Pen      | 3       |
--------------------------------|
2          | Book     | 2       |
--------------------------------|
3          | Phone    | 3       |
--------------------------------|
4          | Computer | 1       |
--------------------------------|
5          | Bag      | 3       |
--------------------------------|
6          | Watch    | 2       |
--------------------------------|
7          | Glasses  | 4       |
--------------------------------|

The result that I need is:

sothorn | 1
Daly    | 2
Lyhong  | 3
Chantra | 1

I have tried this :

    $this->db->select('count(SalerName) as sothorn where tblSaler.SalerID = 1, count(SalerName) as Daly where tblSaler.SalerID = 2, count(SalerName) as Lyhong where tblSaler.SalerID = 3, count(SalerName) as Chantra where tblSaler.SalerID = 4');
    $this->db->from('tblSaler');
    $this->db->join('tblProduct', 'tblSaler.SalerID = tblProduct.SalerID');
like image 865
Bong Thorn Avatar asked Oct 28 '13 05:10

Bong Thorn


People also ask

How do I count items in MySQL?

To counts all of the rows in a table, whether they contain NULL values or not, use COUNT(*). That form of the COUNT() function basically returns the number of rows in a result set returned by a SELECT statement.

What is count (*) in MySQL?

MySQL COUNT() Function The COUNT() function returns the number of records returned by a select query.

How can I count rows in CodeIgniter 4?

Getting a count of table or query rows in CodeIgniter 4 is quite easy using either of the Query Builder countAll() or countAllResults() functions.


2 Answers

You can use this query for this

SELECT
  tblSaler.SalerName,
  count(tblProduct.ProductID) as Total
FROM tblSaler
  LEFT JOIN tblProduct
    ON tblProduct.SalerID = tblSaler.SalerID
GROUP BY tblSaler.SalerID

And here is the active record for this

$select =   array(
                'tblSaler.SalerName',
                'count(tblProduct.ProductID) as Total'
            );  
$this->db
        ->select($select)
        ->from('tblSaler')
        ->join('tblProduct','Product.SalerID = tblSaler.SalerID','left')
        ->group_by('tblSaler.SalerID')
        ->get()
        ->result_array();

Demo

OUTPUT

_____________________
| SALERNAME | TOTAL |
|-----------|-------|
|   sothorn |     1 |
|      Daly |     2 |
|    Lyhong |     3 |
|   Chantra |     1 |
_____________________           
like image 147
Muhammad Raheel Avatar answered Oct 14 '22 08:10

Muhammad Raheel


Please try this code. Its working fine for me and it will help you also.

$this->db->select('SalerName, count(*)');
$this->db->from('tblSaler');        
$this->db->join('tblProduct', 'tblSaler.SalerID = tblProduct.SalerID'); 
$this->db->group_by('tblSaler.SalerID');       
$query = $this->db->get();

You can get whole SQL query using this line below

$query = $this->db->get(); 
echo $this->db->last_query();
like image 41
jagruti Avatar answered Oct 14 '22 09:10

jagruti