开发者

Problem in reading data from multiple tables [duplicate]

开发者 https://www.devze.com 2023-04-04 04:05 出处:网络
This question already has answers he开发者_StackOverflow社区re: Closed 11 years ago. Possible Duplicate:
This question already has answers he开发者_StackOverflow社区re: Closed 11 years ago.

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()


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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消