嗨,我怎样才能进行这样的查询。如果用户已登录,即使未发布,也可以获取所有评论,以便登录用户可以编辑或删除自己的评论,并且所有其他已发布的评论应按 ID 按顺序显示。
我正在使用 CodeIgniter 事件记录。查询如下,其中publish=1,可以看到,publish=0,则看不到。
$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");
但是通过这个查询我无法得到任何结果。
最佳答案
根据您的要求,您必须使用或
条件
尝试这个查询
$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用于演示
关于Mysql查询得到带条件的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39553657/