gpt4 book ai didi

php - 根据 codeigniter 中另一个 mysql 表的 post_id 匹配获取 session user_id 结果

转载 作者:行者123 更新时间:2023-11-29 10:25:34 25 4
gpt4 key购买 nike

我创建了一个函数来计算基于下面 2 个数据库表的帖子总数

1-帖子:

    post_id user_id 
=========== ====
61 122
62 122
96 122
97 122
98 122

2-post_meta:

   meta_id  post_id   status 
======== ======= ======
1 61 1
2 62 0
3 62 1
4 91 1
5 97 1
6 98 1

由于 user_id 存在于 posts 表中,而两个表上的唯一标识符都是 post_id 我希望计算 status = 1 且 user_id = $user_id 的所有帖子这是迄今为止我的模型函数

/**
* The function get_all_user_post_count gets all count posts for admin dashboard
*
* @return array with posts or false
*/
public function get_all_user_post_count($user_id) {
$this->db->select('network_name,COUNT(meta_id) as number', false);
$this->db->from('posts_meta');
$this->db->join('posts', 'posts_meta.post_id=posts.post_id', 'left');
$this->db->where(['status' => '1', 'user_id' => $user_id]);
$this->db->group_by('network_name');
$this->db->order_by('network_name');
$query = $this->db->get();
if ($query->num_rows() > 0) {
$result = $query->result();
// Create new array
$new_array = [];
foreach ($result as $data) {
$new_array[$data->network_name] = $data->number;
}
return $new_array;
} else {
return false;
}
}

这是到目前为止我的 Controller 功能

// Count all sent posts per social network
$count_sent_posts = $this->posts->get_all_user_post_count($this->user_id);
$this->footer = [
'statistics' => $statistics,
'sent' => $count_sent_posts
];
$this->user_layout();

A Database Error Occurred Error Number: 1052

Column 'status' in where clause is ambiguous

SELECT network_name, COUNT(meta_id) as number FROM posts_meta LEFT JOIN posts ON posts_meta.post_id=posts.post_id WHERE status = '1' AND user_id = '118' GROUP BY network_name ORDER BY network_name

Filename: models/Posts.php

Line Number: 393

最佳答案

试试这个 =>

public function get_all_user_post_count($user_id) {
$where = array('posts_meta.status ' => 1 , 'posts.user_id ' => $user_id);
$this->db->select('posts_meta.network_name,COUNT(posts_meta.meta_id) as number', false);
$this->db->from('posts_meta');
$this->db->join('posts', 'posts_meta.post_id=posts.post_id', 'left');
$this->db->where($where);
$this->db->group_by('posts_meta.network_name');
$this->db->order_by('posts_meta.network_name');
$query = $this->db->get();
if ($query->num_rows() > 0) {
$result = $query->result();
// Create new array
$new_array = [];
foreach ($result as $data) {
$new_array[$data->network_name] = $data->number;
}
return $new_array;
} else {
return false;
}
}

关于php - 根据 codeigniter 中另一个 mysql 表的 post_id 匹配获取 session user_id 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48366916/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com