2017-03-12 6 views
1

mysqlに6個のテーブルがあり、すべてのフィールドに 'works_id'という固有のフィールドがあります。これらのテーブルにデータを入力します。表示されている値と一致するだけですが、16行かかります。労働者の名前フィールドに2回の名前が8回繰り返されているとしますが、2回の名前を1回だけ必要とします。 2行かかります。しかし、どのように可能です。私の表の表示:codeigniterでsqlの一致する値のみを選択する

 
Ser Works ID Date Of Works Infrastructure Name of System Type of Works  Vendor Name  Worker Name  
1 1016 2017-03-12  Server   JoinBDApps  Trobleshooting  Vintage IT  Md Rajaon 
2 1016 2017-03-12  Server   JoinBDApps  Software Upgrade Vintage IT  Md Rajaon 
3 1016 2017-03-12  Network   JoinBDApps  Trobleshooting  Vintage IT  Md Rajaon 
4 1016 2017-03-12  Network   JoinBDApps  Software Upgrade Vintage IT  Md Rajaon 
5 1016 2017-03-12  Server   CMH    Trobleshooting  Vintage IT  Md Rajaon 
6 1016 2017-03-12  Server   CMH    Software Upgrade Vintage IT  Md Rajaon 
7 1016 2017-03-12  Network   CMH    Trobleshooting  Vintage IT  Md Rajaon 
8 1016 2017-03-12  Network   CMH    Software Upgrade Vintage IT  Md Rajaon 
9 1016 2017-03-12  Server   JoinBDApps  Trobleshooting  GP IT   Md Forkan 
10 1016 2017-03-12  Server   JoinBDApps  Software Upgrade GP IT   Md Forkan 
11 1016 2017-03-12  Network   JoinBDApps  Trobleshooting  GP IT   Md Forkan 
12 1016 2017-03-12  Network   JoinBDApps  Software Upgrade GP IT   Md Forkan 
13 1016 2017-03-12  Server   CMH    Trobleshooting  GP IT   Md Forkan 
14 1016 2017-03-12  Server   CMH    Software Upgrade GP IT   Md Forkan 
15 1016 2017-03-12  Network   CMH    Trobleshooting  GP IT   Md Forkan 
16 1016 2017-03-12  Network   CMH    Software Upgrade GP IT   Md Forkan 

しかし、私が欲しい:

 
Ser Works ID Date Of Works Infrastructure Name of System Type of Works Vendor Name  Worker Name  
1 1016 2017-03-12  Server   JoinBDApps  Trobleshooting Vintage IT  Md Rajaon 
          Network   CMH    Software Upgrade GP IT   Md Forkan 

マイCodeIgniterのモデルは次のとおりです。

 
$this->db->select('trxn_tbl.works_id, trxn_tbl.works_date, infrashtructure_txn_info.infrashtructure_name, apps_txn_tbl.apps_name, works_type_txn_tbl.works_type, workers_tbl.vendor_id, workers_tbl.name'); 
     $this->db->from('infrashtructure_txn_info'); 
     $this->db->join('workers_tbl', 'trxn_tbl.works_id = workers_tbl.works_id'); 
     $this->db->join('works_type_txn_tbl', 'trxn_tbl.works_id = works_type_txn_tbl.works_id'); 
     $this->db->join('infrashtructure_txn_info', 'trxn_tbl.works_id = infrashtructure_txn_info.works_id'); 
     $this->db->join('apps_txn_tbl', 'trxn_tbl.works_id = apps_txn_tbl.works_id'); 
     $query = $this->db->get(); 

     return $query->result(); 

誰も私を助けてください...

答えて

0

distinct()を入れてあなたのコードのトップは次のとおりです:

$this->db->distinct(); 
$this->db->select('trxn_tbl.works_id, trxn_tbl.works_date, infrashtructure_txn_info.infrashtructure_name, apps_txn_tbl.apps_name, works_type_txn_tbl.works_type, workers_tbl.vendor_id, workers_tbl.name'); 
...rest of your code... 

これは役に立ちますか?

+0

その出力も同じです。 16行のように.... –

+0

'$ this-> db-> distinct()の後に' $ this-> db-> group_by( 'name_of_column'); 'を追加します 今ですか? –

+0

次に表示されます...... SELECTリストの式#1はGROUP BY句にはなく、GROUP BY句のカラムに機能的に依存しない非集約カラム 'dcapp_db.trxn_tbl.works_id'を含みます。これはsql_mode = only_full_group_byと互換性がありません –

1

あなたは行を集約するGROUP BYを使用する必要があります。これが動作するかどうか

$this->db->group_by(array('trxn_tbl.works_id', 'infrashtructure_txn_info.infrashtructure_name')); 

は試してみてください。

$this->db->select('trxn_tbl.works_id, trxn_tbl.works_date, infrashtructure_txn_info.infrashtructure_name, apps_txn_tbl.apps_name, works_type_txn_tbl.works_type, workers_tbl.vendor_id, workers_tbl.name'); 
$this->db->from('infrashtructure_txn_info'); 
$this->db->join('workers_tbl', 'trxn_tbl.works_id = workers_tbl.works_id'); 
$this->db->join('works_type_txn_tbl', 'trxn_tbl.works_id = works_type_txn_tbl.works_id'); 
$this->db->join('infrashtructure_txn_info', 'trxn_tbl.works_id = infrashtructure_txn_info.works_id'); 
$this->db->join('apps_txn_tbl', 'trxn_tbl.works_id = apps_txn_tbl.works_id'); 

$this->db->group_by(array('trxn_tbl.works_id', 'infrashtructure_txn_info.infrashtructure_name')); 

$query = $this->db->get(); 

return $query->result(); 

EDIT:をごGROUP_BY関連のエラーを取得した場合は、すべてのことで、グループにしてみてください選択した列の名前付き列。もう1つの方法は、group_by句に含まれていないすべての列にGROUP_CONCATを使用することです。例:GROUP_CONCAT(workers_tbl.name SEPARATOR ",") as workers_tbl.nameなど