我有 3 个值:
- 对象 ID
- 用户电子邮件
- 用户密码
还有两张表:
对象:
- 对象编号
- 用户身份
- 对象名称
用户:
- 用户身份
- 电子邮件
- 密码
如果出现以下情况,我想选择对象的名称:
- 用户有效(电子邮件和通过返回行)
- 并且对象属于用户或用户id = 0(属于所有人)
- object.id = 对象ID
什么sql语句可以让我知道?
我有 3 个值:
还有两张表:
对象:
用户:
如果出现以下情况,我想选择对象的名称:
什么sql语句可以让我知道?
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
未经测试,但应该可以工作。
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
)
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?
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
如果您想测试,我将对象放在 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