我在mysql中还有6个表,每个字段都有一个唯一的字段' works_id',我输入数据这些表。我想只显示匹配的值,但需要16行。假设在工人姓名字段中显示两个名称,重复8次,但我只想要两个名字一次。它可能需要两行。但它是如何可能的。我的表格显示:
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 :(得分: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();
编辑:如果您收到任何与group_by相关的错误,请尝试按选择中的所有命名列进行分组。另一种方法是尝试将GROUP_CONCAT
用于group_by子句中未包含的所有列。例如:GROUP_CONCAT(workers_tbl.name SEPARATOR ",") as workers_tbl.name
等。
答案 1 :(得分: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...
这有帮助吗?