私はCodeIgniterでWebアプリケーションを構築しています。1つのSQLクエリで複数のカウントがあります
ユーザーは投稿を「愛する」または「憎む」ことができます。これらのアクションは、テーブルに格納されている次の列でpost_rating呼ば:
- ID
評価はどちらかの愛のために中立のための0、1することができ嫌いの場合は2です。私のモデルでは
は、私は次の関数を各ポストのためにいくつかの基本的な情報を返した:
function get_posts($thread_id)
{
$this->db->select('id, user_id, date_posted, content');
$this->db->from('post');
$query = $this->db->get();
if ($query->num_rows() > 0)
{
return $query->result();
}
}
私はpost_ratingテーブルに参加する必要があり理解し、どのように私はまた、愛を返すについては行くだろうとタイトル、コンテンツなどと同じ配列の嫌悪感を数えますか?
ありがとうございます!
:)
UPDATE!ここで
は、現時点では私のモデルです:
function get_posts($thread_id)
{
$this->db->select('post.id, post.user_id, post.date_posted, post.content, post.status_visible, user.username, user.location, user.psn, user.clan, user.critic, user.pro, SUM(case when rating = 1 then 1 end) as love, SUM(case when rating = 2 then 1 end) as hate');
$this->db->from('post');
$this->db->join('user', 'user.id = post.user_id', 'left');
$this->db->join('post_rating', 'post_rating.post_id = post.id', 'left');
$this->db->where('thread_id', $thread_id);
$this->db->order_by('date_posted', 'asc');
$query = $this->db->get();
if ($query->num_rows() > 0)
{
$this->db->select('id');
$this->db->from('post_vote');
return $query->result();
}
}
より賢明な評価方式は0になりますニュートラル、愛のために1、嫌いに-1。 –
よろしくお願いします。私はそれを切り替えるかもしれない。ありがとう! – Leeloo
このクエリではあまり違いはありませんが、後ほど便利です。 –