如何为此

时间:2016-03-06 17:27:40

标签: php mysql subquery

我有一张表有字段的表。

id,is_public,added_by,is_delete

情景可能性:

当前用户可以查看记录:

  1. added_by admin = 0 OR own id
  2. added_by another user AND is_public = 1
  3. **is_deleted=0**
  4. 必须显示

    current user's and admin's record is_public='0' 记录

    SQLFIDDLE

    我的疑问:

    SELECT * FROM tblname 
    WHERE 
    is_delete='0' 
    AND (added_by='0' OR added_by='15') //ex. current user id = 15
    AND is_public='1'
    

    但此查询未提供其他用户的is_public='1'记录

    请帮助

3 个答案:

答案 0 :(得分:1)

您需要在选择中使用OR运算符:

SELECT * FROM tblname 
WHERE 
is_delete='0' AND (
   (added_by='0' OR added_by='15') //ex. current user id = 15
   OR is_public='1'
)

如果用户应该看到任何已删除的记录,则可以使用此sql:

SELECT * FROM tblname 
WHERE 
added_by='15' //ex. current user id = 15
OR (
   is_delete='0' AND (added_by='0' OR is_public='1')
);

答案 1 :(得分:1)

怎么样:

let firstName = "David"
// must have 1 or more vowels
let REGEX = "[aeiou]"
let check = NSPredicate(format: "SELF CONTAINS %@", REGEX).evaluateWithObject(firstName)

通过这种方式,您只能展示来自同一用户或公开

的未删除帖子

更新: 如果您希望节目当前用户也删除了帖子(所以用户的所有帖子都是0/14,其他用户只有公开和未删除的帖子:

SELECT * FROM tblname 
WHERE 
is_delete='0' AND (
(added_by='0' OR added_by='15') //ex. current user id = 15
OR is_public='1')

答案 2 :(得分:0)

我尝试了一些并取得了成功,谢谢大家的支持:)

SELECT * FROM tblname 
WHERE 
is_delete = '0' AND 
( user_id = '0' OR user_id = '15' OR is_public = '1' )