加入3个表进行搜索

时间:2013-09-29 14:43:55

标签: sql join

我有3个表:会话,电影和供应商

Session.FilmID
Film.VendorID
Vendor.VendorName

我想在搜索中包含VendarName。

这就是我所拥有的:

sql ="SELECT SessionID, Identifier, SessionType.SessionTypeName, Film.*, Vendor.*, 
CameraID, Date FROM Session "
sql = sql + "JOIN SessionType USING (SessionTypeID) "
sql = sql + "JOIN Film USING(FilmID) "
sql = sql + "JOIN Vendor ON Film.VendorID = Vendor.VendorID "
sql = sql + "WHERE Session.Identifier LIKE ('%" + search + "%') OR "
sql = sql + "Session.Notes LIKE('%" + search + "%') OR "
sql = sql + "SessionType.SessionTypeName LIKE ('%" + search + "%') "
sql = sql + "Film.Vendor.VendorName LIKE ('%" + search + "%') "
sql = sql + "ORDER BY Identifier,SessionType.SessionTypeName"

我做错了什么?

1 个答案:

答案 0 :(得分:1)

您在sql语句中缺少“OR”。改变这一行:

sql = sql + "SessionType.SessionTypeName LIKE ('%" + search + "%') "

要:

sql = sql + "SessionType.SessionTypeName LIKE ('%" + search + "%') OR "