mysql join查询重复返回数据

时间:2014-02-20 07:48:52

标签: c# mysql

我想在c#windows窗体中从mysql数据库中检索数据(使用连接查询),但它会重复给我数据100次。 这是我的代码。请帮忙。

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

2 个答案:

答案 0 :(得分:0)

学生证是两个表中的主键吗?

答案 1 :(得分:0)

您需要一个子查询进行比较

完整的选择语句想成为

"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 +")"