选择、计数和使用 codeigniter 和 mysql 的位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19627475/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Select, count and where using codeigniter and mysql
提问by Bong Thorn
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');
回答by Muhammad Raheel
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();
OUTPUT
输出
_____________________
| SALERNAME | TOTAL |
|-----------|-------|
| sothorn | 1 |
| Daly | 2 |
| Lyhong | 3 |
| Chantra | 1 |
_____________________
回答by jagruti
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
您可以使用下面的这一行获取整个 SQL 查询
$query = $this->db->get();
echo $this->db->last_query();
回答by naveen goyal
Try This
尝试这个
$this->db->select('SalerName, count(*)');
$this->db->from('tblSaler');
$this->db->join('tblProduct', 'tblSaler.SalerID = tblProduct.SalerID');
$this->db->group('SalerID');
回答by Mosin
## try this ##
$this->db->select($column_name);
$this->db->where($column_name,$type);
$q=$this->db->get($table_name);
$count=$q->result();
return count($count);'