我的查询中有多个重复项

时间:2014-01-30 13:05:25

标签: mysql vb.net

我的查询中有重复的信息..我只需要完成的是我有1个文本框,每当我输入内容时,它会搜索名字或姓氏,但问题是每当我在文本框中键入内容时我的查询返回MULTIPLE DATA,这是问题,因为我每条记录只有1个数据。这个查询有什么问题。坚持了2天-_-

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

con.Open()
cmd = New MySqlCommand("SELECT tr.res_lname, tr.res_fname, tr.purok_name, tu.username, 

DATE_FORMAT(tv.transDate,'%M'), tv.status FROM tbl_violation AS tv 
INNER JOIN tbl_resident AS tr 
INNER JOIN tbl_user AS tu WHERE tr.res_id = tv.res_id 
AND tv.encodedbyId = tu.user_id AND tv.status = 0 
AND DATE_FORMAT(tv.transDate,'%M') <> DATE_FORMAT(CURDATE(),'%M') 
AND tr.res_lname like @lname OR tr.res_fname like @fname", con)

cmd.Parameters.AddWithValue("@lname", "%" & TextBox1.Text & "%")
cmd.Parameters.AddWithValue("@fname", "%" & TextBox1.Text & "%")

dr = cmd.ExecuteReader
        dgv.Rows.Clear()
        While dr.Read
            dgv.Rows.Add(dr(0), dr(1), dr(2), dr(3), dr(4), dr(5))
        End While

End Sub

1 个答案:

答案 0 :(得分:0)

发布的当前查询存在潜在问题。我不确定这是否是导致查询返回更多数据的主要问题。

.....
WHERE 
    tr.res_id = tv.res_id 
        AND tv.encodedbyId = tu.user_id 
        AND tv.status = 0 
        AND DATE_FORMAT(tv.transDate,'%M') <> DATE_FORMAT(CURDATE(),'%M') 
        AND tr.res_lname like @lname OR tr.res_fname like @fname

上面的查询将返回满足条件tr.res_fname like @fname的所有行,忽略WHERE子句中的其他过滤器。我想你想要这个

.....
WHERE 
    tr.res_id = tv.res_id 
        AND tv.encodedbyId = tu.user_id 
        AND tv.status = 0 
        AND DATE_FORMAT(tv.transDate,'%M') <> DATE_FORMAT(CURDATE(),'%M') 
        AND (tr.res_lname like @lname OR tr.res_fname like @fname)