2

类似以下的事情是我想要做的

SELECT id, status, Gid, Uid, note FROM notes LIMIT 10

但状态 ID 可能不同,它需要类似于以下内容

SELECT a.id, a.status, a.Gid, a.Uid, a.note
FROM notes as a
WHERE
  IF(a.status < 2, DONT SELECT),
  IF(a.status == 2, 
    (SELECT approved FROM friends WHERE Uid = a.Uid AND Fid = ".$_SESSION['Uid'].") 
      IF(approved = 1, SELECT ROW, else DONT)
  )
  IF(a.status == 3, SELECT)
  IF(a.status == 4,
     (SELECT Gid FROM friends WHERE Uid = a.Uid AND Fid = ".$_SESSION['Uid'].")
     IF(Gid == a.Gid, SELECT ROW, else DONT)
ORDER BY id DESC
LIMIT 10

我查找了 CASE 命令,但不确定如何使用它,但我可以看到这是这样做的方法,但是是的.. 我不知道该怎么做。以上是我正在尝试做的一个示例,a.Uid 可以是不同的 id,所以当状态 == 2 或 4 时,我们需要查看查看用户是已批准的朋友还是在 groupId (Gid )

任何解决此查询的帮助将不胜感激。如果需要,我可以稍微更改一下注释表,因为它尚未投入生产,但不鼓励。我愿意接受建议

提前致谢!

4

1 回答 1

2

我认为这会做你想要的:

select a.id, a.status, a.Gid, A.Uid, a.note
FROM notes a
LEFT JOIN friends f ON f.Uid = a.Uid AND f.Fid = $_SESSION[Uid]
WHERE (a.status = 2 AND f.approved = 1)
OR a.status = 3
OR (a.status = 4 AND f.Gid = a.Gid)
ORDER BY a.id DESC
LIMIT 10
于 2012-11-03T02:56:31.197 回答