我的 sql 表就像
posts-> id postedby timestamp
1 1 12716826
comments-> id postid comby tstamp condition
1 1 1 12716826 me
2 1 1 12716826 all
1 1 2 12716826 me
1 1 3 12716826 all
我的问题是当我进行查询以获取帖子 1 的所有评论时。我想要以下条件
条件为我的评论应该只有我或帖子所有者可见
我做了一个像 $myid=2; 这样的查询。$postbyid=1;
$res=$db->query("SELECT * from comments where postid='$postbyid'
order by tstamp desc");
while ($row=$res->fetch_assoc())
{
if($row['comby']==$uid || $row['comby']=='$postbyid')
{
$data[]=$row;
}
else
{
if($row['condition']!='me')
{
$data[]=$row;
}
}
}
这可能是错误的。有没有办法做到这一点SQL CASE
?
喜欢
SELECT * from comments where postid='$postbyid' CASE WHEN comby='$myid'
THEN SHOW ALL WHEN combyid='$postbyid' THEN SHOW ALL else dont
show this row order by tstamp desc