我在数据库中有多个表。我正在搜索所有表格以查找特定条目。搜索结果正确返回。但是我怎么知道哪个表包含 MySql 中的特定数据/条目?有没有办法在搜索多个表时返回/知道包含特定条目的表名?
Dim statusQuery As String = $"SELECT * FROM management.hostelA,management.hostelB,management.hostelC,management.hostelD where hostelA.occupant1='{stu_name.Text}' or hostelA.occupant2='{stu_name.Text}' or hostelB.occupant1='{stu_name.Text}' or hostelB.occupant2='{stu_name.Text}' or hostelC.occupant1='{stu_name.Text}' or hostelC.occupant2='{stu_name.Text}' or hostelD.occupant1='{stu_name.Text}' or hostelD.occupant2='{stu_name.Text}'"
cmd = New MySqlCommand(statusQuery, con)
Dim sdr As MySqlDataReader
sdr = cmd.ExecuteReader
While sdr.Read
statusflag = statusflag + 1
End While
我想知道哪个表包含 stu_name.text
最佳答案
您最好单独查询每个表,即一次查询一个表。然后你会很容易地看到这个名字是在哪个表中找到的。
尽管来自@Uueerdo 的评论,您并没有执行一堆UNION
操作,您正在执行一堆JOIN
操作。这意味着您的查询正在做一个巨大的 Cartesian product四张 table 。换句话说,所有四个表的每一行都与所有其他表的每一行匹配。我确定这不是您想要的。
在你尝试做这样的事情之前,你需要学习 SQL。
在那之前,您应该一次查询一个表。
关于mysql - 在 MySql 中搜索多个表时,有什么方法可以知道哪个表包含特定条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45700807/