列出所有朋友

时间:2009-08-22 23:19:12

标签: sql sql-server asp-classic

我在FRIENDS表中有F.FRIENDID,F.MEMBERID列,在MEMBERS表中有M.MEMBERID,M.FIRSTNAME,M.LASTNAME。我想列出所有已登录成员的朋友。这里的问题是,在成员ID中签名有时可以在FRIENDID行或FRIENDS表中的MEMBERID行中。这是因为当会员添加其他会员时,他或她的ID会记录在FRIENDID字段中,其他会员的ID会记录在MEMBERID字段中。

我尝试了几个SQL查询。都没有奏效。我认为这个查询应该有效,但它会出现以下错误。

SQL = "SELECT F.FRIENDID, F.MEMBERID, F.ACTIVE, M.MEMBERID, M.FIRSTNAME, M.LASTNAME, M.ACTIVE"
SQL = SQL & " FROM FRIENDS F, MEMBERS M"
SQL = SQL & " WHERE (F.FRIENDID OR F.MEMBERID) = "& Session("MEMBERID") &" AND (F.FRIENDID OR F.MEMBERID) = M.MEMBERID AND F.ACTIVE = 1 AND M.ACTIVE = 1"
SQL = SQL & " ORDER BY M.FIRSTNAME, M.LASTNAME ASC"
Set objFriends = objConn.Execute(SQL)   

Microsoft OLE DB Provider for ODBC Drivers错误“80040e14”

[Microsoft] [ODBC SQL Server驱动程序] [SQL Server]关键字“OR”附近的语法不正确。

4 个答案:

答案 0 :(得分:1)

你无法在SQL中执行:AND (F.FRIENDID OR F.MEMBERID) = M.MEMBERID

将其更改为:

AND (F.FRIENDID = M.MEMBERID OR F.MEMBERID = M.MEMBERID)

答案 1 :(得分:1)

您需要使用IN语法:

WHERE M.MEMBERID IN (F.FRIENDID, F.MEMBERID)
  AND "& Session("MEMBERID") &" IN (F.FRIENDID, F.MEMBERID)

以下是我使用ANSI连接重写查询的方法:

SELECT f.friendid,
       f.memberid,
       f.active,
       COALESCE(mf.memberid, mm.memberid) 'memberid',
       COALESCE(mf.firstname, mm.firstname) 'firstname',
       COALESCE(mf.lastname, mm.lastname) 'lastname',
       COALESCE(mf.active, mm.active) 'active'
  FROM FRIENDS f
  JOIN MEMBERS mf ON mf.memberid = f.friendid AND mf.active = 1
  JOIN MEMBERS mm ON mm.memberid = f.memberid AND mm.active = 1
 WHERE f.active = 1
   AND "& Session("MEMBERID") &" IN (f.friendid, f.memberid)
 ORDER BY firstname, lastname

答案 2 :(得分:0)

你试过这个吗?

SQL = SQL & " WHERE (F.FRIENDID = " & Session("MEMBERID") &
            " OR F.MEMBERID = "& Session("MEMBERID") &
            ") AND (F.FRIENDID = M.MEMBERID OR F.MEMBERID = M.MEMBERID) "&
            "AND F.ACTIVE = 1 AND M.ACTIVE = 1"

答案 3 :(得分:0)

您需要查看SQL表达式的语法 OR只能在表达式之间使用

e.g  X = Z或Y = Z. 不  (X或Y)= Z

所以重做那样的SQL,它应该工作.. 我发现在嵌入VB错误消息之前在命令行执行SQL会有所帮助