0

我有 3 个值:

  • 对象 ID
  • 用户电子邮件
  • 用户密码

还有两张表:

  • 对象:

    • 对象编号
    • 用户身份
    • 对象名称
  • 用户:

    • 用户身份
    • 电子邮件
    • 密码

如果出现以下情况,我想选择对象的名称:

  • 用户有效(电子邮件和通过返回行)
  • 并且对象属于用户或用户id = 0(属于所有人)
  • object.id = 对象ID

什么sql语句可以让我知道?

4

5 回答 5

1

Return only the object name?

SELECT object_name 
FROM objects o LEFT JOIN users u ON o.user_id = u.user_id
WHERE (u.email IS NOT NULL
AND u.password IS NOT NULL) 
OR user_id = 0
于 2013-05-29T14:18:32.383 回答
1

未经测试,但应该可以工作。

SELECT 
    o.object_name 

FROM
    objects o
    LEFT JOIN users u ON o.user_id = u.user_id

WHERE
    o.objectId = 1
    AND (
        (u.email = ''
        AND u.password = '')
        OR o.user_id = 0
    )
于 2013-05-29T14:15:31.783 回答
1
SELECT o.object_name FROM objects o
LEFT JOIN users u ON o.user_id=u.user_id
WHERE (u.email IS NOT NULL AND u.password IS NOT NULL)
OR o.user_id=0

我不确定您是指 object_name 还是 object_id?

于 2013-05-29T14:21:45.547 回答
0
SELECT 
   o.Name
FROM
   Objects o
LEFT JOIN
   Users u
ON
   u.Id = o.UserId
WHERE
   (u.Email = email
AND
   u.Password = password)
OR
   o.UserId = 0
于 2013-05-29T14:25:45.590 回答
0

如果您想测试,我将对象放在 SqlFiddle 中。

http://sqlfiddle.com/#!2/86023/9

SELECT O.ID FROM OBJECTS O

 WHERE O.ID IN  (
SELECT U.ID FROM USERS U
WHERE  U.USER = 'test' AND U.PASS = 'test'
)

OR O.USERID = 0
于 2013-05-29T14:28:06.470 回答