大家好,我有这些表格;
USERS( user_id ,fullname ,username etc.)
POSTS ( post_id, user_id, post, orig_post_id, replyto date)
USER_PROFILE (id, user_id, profile_image_path etc)
实施例
USERS (1 ,John Doe ,johndoe etc.),( 2 ,Stack Flow ,stackflow etc.)
POSTS (2, 1, My naame is John doe and i approve this message, 0, 0,sometimestamp),
(3, 12, My naame is Stack Flow and i approve this message, 0, 2,sometimestamp)
USER_PROFILE (1, 1, ppdjodjf.jpg etc),(2, 2, grsdjodjf.jpg etc)
基本上,如果replyto字段为0
,我希望查询输出array('post_id' => 2,
'user_id' => 1,
'post' => the post,
'orig_post_id,' => 0
'replyto,' => 0
username => johndoe,
fullname => John Doe,
profile_image_path => etc)
当它不为零时
array('post_id' => 3,
'user_id' => 2,
'post' => Another post,
'orig_post_id,' => 0
'replyto,' => 2
username => stackflow,
fullname => Stack Flow,
profile_image_path => etc,
'replies' => array(all the replies)
答案 0 :(得分:1)
这些是SQL的基础知识。你真的应该学习一些SQL。 它很容易学习,你可以花半个小时,这将是非常有益的。
首先,给定用户的帖子是:
select posts.*
from posts
where posts.user_id = '$user_id'
要获取所需的用户字段,请执行连接
select posts.*,users.username,users.fullname
from posts
inner join users where posts.user_id = users.user_id
where posts.user_id = '$user_id'
您应该能够弄清楚如何加入user_profile来获取这些字段。
要仅过滤那些没有orig_post_id的记录,您可能需要测试零, 或者您可能需要测试NULL。也许两者都是,所以假设你想测试两者:
where posts.user_id = '$user_id'
and (orig_post_id = 0 or orig_post_id is null)
答案 1 :(得分:1)
花了一整天的时间,我的代码最终看起来像这样。
public function get_connected_post()
{
$user_id = $this->session->userdata('user_id');
$sql = "SELECT p.*,up.fullname,u.username,upi.file_path_thumb,IF(hi5c.hi5_count is NULL,'0',hi5c.hi5_count) AS count_hi5,
IF(brn.branch_count is NULL,'0',brn.branch_count) AS count_branch, IF(rply.reply_count is NULL,'0',rply.reply_count) AS count_reply,
IF((SELECT COUNT(*) FROM post_highfives ph WHERE ph.user_id = $user_id AND ph.post_id = p.post_id),'1','0') AS count_is_hi5ed,
IF((SELECT COUNT(*) FROM posts pt WHERE pt.user_id = $user_id AND pt.is_branch_of_id = p.post_id),'1','0') AS count_is_branched
FROM (
SELECT user_id
FROM user_followers
WHERE follower_id = $user_id
UNION ALL
SELECT $user_id
) uf
JOIN posts p
ON p.user_id = uf.user_id
JOIN user_profile up
ON up.user_id = p.user_id
JOIN user_profile_images upi
ON upi.image_id = up.profile_image_id
JOIN users u
ON u.user_id = p.user_id
LEFT OUTER JOIN (SELECT ph.post_id, count(*) AS hi5_count
FROM post_highfives ph
GROUP BY ph.post_id) hi5c
ON p.post_id = hi5c.post_id
LEFT OUTER JOIN (SELECT pst.post_id,pst.is_branch_of_id, count(*) AS branch_count
FROM posts pst
GROUP BY pst.is_branch_of_id) brn
ON p.post_id = brn.is_branch_of_id
LEFT OUTER JOIN (SELECT pst.post_id,pst.reply_to, count(*) AS reply_count
FROM posts pst
GROUP BY pst.reply_to) rply
ON p.post_id = rply.reply_to
ORDER BY p.post_date DESC";
$query = $this->db->query($sql);
if ($query) {
$result = array();
foreach($query->result_array() as $r){
$branch_id = $r['is_branch_of_id'];
if($branch_id != 0){
$branch_array = $this->branch_query($this->postid_return_user_id($branch_id),$branch_id);
}else{
$branch_array = array();
}
$result[] = array_merge((array)$branch_array, (array)$r);
}
return $result;
}
else {
return false;
}
}
我知道这看起来与我提出的问题有所不同,但我试图简化我实际做的事情。我不认为问一个问题,说明这个问题会让我无处可去,尽管简单地提出这个问题仍然没有得到我在任何地方都没有人理解我哈哈。对于我的问题无论如何,相关的部分是代码的最后一位,我把条件ssaying如果branch_id不为零获取一些数组数据,如果它返回一个空数组。我合并了数组与查询结果。 现在我必须想到我怎么能简单地做到这一点。