2014-02-20 30 views
2

我想在c#窗體中從mysql數據庫中檢索數據(使用連接查詢),但它重複給我100次數據。 這是我的代碼。請幫忙。mysql連接查詢重複返回數據

private void button2_Click(object sender, EventArgs e) 
     { 
      if (comboBox1.SelectedIndex < 0 || comboBox2.SelectedIndex < 0) 
      { 
       MessageBox.Show("Please select the Class and group"); 
      } 
      else 
      { 
       table = dbOperation.select("student.admission_no, student.studid, student.name from student inner join " + 
              "studentinclass on studentinclass.studentid = student.studid inner join " + 
              "class on studentinclass.classid = " + comboBox1.SelectedValue + " inner join " + 
              "`group` on studentinclass.groupid = " + comboBox2.SelectedValue + " inner join " + 
              "section on studentinclass.sid = " + comboBox3.SelectedValue); 
       listView1.Items.Clear(); 

       foreach (DataRow row in table.Rows) 
       { 
        listView1.Items.Add(row[0].ToString()); 
        listView1.Items[listView1.Items.Count - 1].SubItems.Add(row[1].ToString()); 
        listView1.Items[listView1.Items.Count - 1].SubItems.Add(row[2].ToString()); 
        listView1.Items[listView1.Items.Count - 1].SubItems.Add("P"); 
       } 
      } 
     } 

這是輸出

enter image description here

+1

什麼是您所期望的輸出? BTW在Select關鍵字後添加'DISTINCT'將幫助您消除所有重複行。 – Neels

+0

dbOperation是什麼類型? –

+0

這是數據庫連接類的對象。 – Loyal

回答

0

是學生ID在這兩個表的主鍵?

+0

學生證是否唯一標識兩個表中的每條記錄? – suryakrish

0

您將需要一個子查詢比較

完整的select語句要成爲

"student.admission_no, student.studid, student.name from student " + 
"where student.studid in (select studentinclass.studentid from studentinclass " + 
"inner join class on studentinclass.classid = " + comboBox1.SelectedValue + " " + 
"inner join `group` on studentinclass.groupid = " + comboBox2.SelectedValue + " " + 
"inner join section on studentinclass.sid = " + comboBox3.SelectedValue +")"