2017-08-16 15 views
1

これは私の詳細テーブルである:mysqlでデータの繰り返しを削除するには?

enter image description here

これは私のコンタクト表である:ここ

enter image description here

詳細テーブルと4行(各詳細2を有する)に2列です連絡先テーブルにあります。私は結合クエリを使用するとき私は4つの結果の行を取得するが、私は2行(1行の詳細行とその対応する詳細の1つの連絡先)したい。
私のクエリ:select('*')の代わりにselect('DISTINCT *')を使用して

$this->db->select('*'); 
$this->db->from('dots_center_detail'); 
$this->db->join('dots_center_contact', 'dots_center_contact.registration_id = dots_center_detail.registration_id','left'); 

答えて

1

この

$this->db->query(" 
    SELECT DISTINCT dots_center_detail.registration_id, dots_center_contact.contact 
    FROM dots_center_detail 
    LEFT JOIN dots_center_contact ON dots_center_contact.registration_id = dots_center_detail.registration_id 
") 
を試してみてください
1

は、必要な結果を与えることができます。それは試みに値します。

または、このようなビューを作成してテーブル定義を作成することができます。

CREATE OR REPLACE VIEW dots_center_unique_contact AS 
    SELECT DISTINCT * FROM dots_center_contact; 

次に、結合操作でそのdots_center_unique_contactビューを参照してください。

長期的には、重複する行がある理由を理解し、ビジネスルールを強化しないようにすることをお勧めします。

関連する問題