从多个表读取数据的问题

发布于 2024-12-04 05:10:34 字数 1219 浏览 1 评论 0原文

可能的重复:
datareader 检索数据时出现问题

我在数据库中有会员资格和预订表,其中包含属性 cust_id是会员资格中的主键和预订中的参考键。当我执行数据读取器时,我希望它从会员表中读取 cust_id 值,但它是从预订表中读取它......

我加入表的一个主要目的是还有其他验证(不关心这个问题..刚刚提到了加入表的原因),它需要预订表中的属性,例如我必须检查新预订是否与某些已经进行的预订重叠......

我在会员资格中有3个唯一的cust_id值表即预订表中的 csc-01、csc-02、csc-03 和 6 个预订均适用于 csc-01 和 csc-02,并且没有针对 cust_id csc-03 的客户的预订...当我从会员资格中进行选择时,即然后是 Memberships.cust_id ,因为我使用消息框来检查结果,在 MessageBox.Show(str1 & "," & str3 & String.Equals(str1, str3)) str3 从不显示值 csc-03,但它存在于 MEmberships 表中...我希望 bookchk 迭代 Memberships.cust_id 中的所有值,以便每次检查客户是否为客户是否拥有会员资格......我希望您现在已经理解

我的问题是:

str2 = "select Memberships.cust_id from Memberships, Bookings where   Memberships.cust_id  = Bookings.cust_id"
Dim cmd2 As New SqlCommand(str2, con)
con.Open()

Dim bookchk As SqlDataReader = cmd2.ExecuteReader  
While bookchk.Read()
Dim str1 As String = MskdTxtCustId.Text
Dim str3 As String = bookchk("cust_id")
MessageBox.Show(str1 & "," & str3 & String.Equals(str1, str3))

End While
bookchk.Close()
con.Close()

Possible Duplicate:
Problem in datareader retrieving data

I have Memberships and Bookings tables in database containing an attribute cust_id which is primary key in Memberships and reference key in Bookings. When i am executing data reader I want it to read cust_id values from membership table but it is reading it from the bookings table.....

One major purpose for which i have joined the tables is that there are other validations (not concerned with this problem..just mentioned the reason of joining table) which need attributes from the bookings tables like for example i have to check whether new booking overlaps with some already made booking or not....

I have 3 unique cust_id values in the Memberships table i.e. csc-01,csc-02,csc-03 and 6 Bookings in Bookings table all are for csc-01 and csc-02 and there is no booking for customer with cust_id csc-03...When I'm selecting from Memberships i.e. Memberships.cust_id then, as i have used message box to check the results, in MessageBox.Show(str1 & "," & str3 & String.Equals(str1, str3)) str3 never shows a value csc-03 but it is present in MEmberships table...i want bookchk to iterate through all the values in Memberships.cust_id so that every time it is checked whether a customer has membership or not....I hope you have understood now

My query is:

str2 = "select Memberships.cust_id from Memberships, Bookings where   Memberships.cust_id  = Bookings.cust_id"
Dim cmd2 As New SqlCommand(str2, con)
con.Open()

Dim bookchk As SqlDataReader = cmd2.ExecuteReader  
While bookchk.Read()
Dim str1 As String = MskdTxtCustId.Text
Dim str3 As String = bookchk("cust_id")
MessageBox.Show(str1 & "," & str3 & String.Equals(str1, str3))

End While
bookchk.Close()
con.Close()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

给不了的爱 2024-12-11 05:10:34
select Memberships.cust_id from Memberships, Bookings where   
Memberships.cust_id  = Bookings.cust_id
union
Memberships.cust_id from Memberships where 
not exists (select Bookings.cust_id from Bookings where
Memberships.cust_id  = Bookings.cust_id)

如果在第一个选择中添加更多列,在第二个选择中添加常量 0 或空白,则行数必须相等

select Memberships.cust_id from Memberships, Bookings where   
Memberships.cust_id  = Bookings.cust_id
union
Memberships.cust_id from Memberships where 
not exists (select Bookings.cust_id from Bookings where
Memberships.cust_id  = Bookings.cust_id)

if you add more columns in the first select, add constants 0 or blank in the second, the number of rows must be equal

节枝 2024-12-11 05:10:34

所以您想查看没有预订的 csc-03。答案就在重复的问题中——LEFT JOIN

SELECT Memberships.cust_id, Bookings.*
FROM Memberships LEFT JOIN Bookings
     ON Memberships .cust_id = Bookings.cust_id

So you want to see csc-03 who has no bookings. The answer is there in the duplicate question -- LEFT JOIN

SELECT Memberships.cust_id, Bookings.*
FROM Memberships LEFT JOIN Bookings
     ON Memberships .cust_id = Bookings.cust_id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文