首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Mysql查询获取带条件的结果

Mysql查询获取带条件的结果
EN

Stack Overflow用户
提问于 2016-09-18 11:07:53
回答 1查看 46关注 0票数 0

嗨,我怎样才能像这样进行查询呢?如果用户已登录,则获取所有评论,即使它未发布,以便登录用户可以编辑或删除此自己的评论,也应按ID顺序显示所有其他已发布的评论。

我正在使用CodeIgniter active record。查询如下,其中publish =1,它是可见的,而publish =0看不到。

代码语言:javascript
运行
复制
 $this->db->select('user.name,user.image,product_review.*')->join('user', 'user.user_id = product_review.user_id');
 if ($this->session->userdata("is_logged_in")) {
        $this->db->where('product_review.user_id', $this->session->userdata("user_id"));
        $this->db->where('publish', '0');
 }
 $this->db->where('publish', '1');
 $this->db->where('product_id', $product_id);
 $this->db->order_by('review_id', "DESC");
 $query = $this->db->get("product_review");

但是对于这个查询,我不能得到任何结果。

EN

Stack Overflow用户

发布于 2016-09-18 11:41:41

根据您的要求,您必须使用or条件

尝试此查询

代码语言:javascript
运行
复制
$this->db->select('user.name, user.image, product_review.*');
$this->db->from('product_review');
$this->db->join('user', 'user.user_id = product_review.user_id');
if ($this->session->userdata("is_logged_in")) {
    $user_id = $this->session->userdata("user_id");
    $this->db->where("(product_review.user_id = $user_id or product_review.publish = 1)");
} else {
    $this->db->where('product_review.publish', '1');
}
$this->db->where('product_review.product_id', $product_id);
$this->db->order_by('product_review.review_id', "DESC");
$result = $this->db->get()->result();

用于演示的click there

票数 1
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39553657

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档