Codeigniter join重复返回的值 [英] Codeigniter join repeats returned values
问题描述
我有3个数据库表,视频,事件和用户。
我的问题是如果我加入这3个表,并显示在我的视图中的结果它重复。
例如如果我echo在我的配置文件中的用户名,我上传7视频显示名称七次。
function get_profile($ id)
{
$ this-> db-> select('*');
$ this-> db-> from('pf_users');
$ this-> db-> join('pf_videos','pf_videos.uid = pf_users.uid');
$ this-> db-> join('pf_events','pf_events.uid = pf_users.uid');
$ this-> db-> where('pf_users.uid',$ id);
$ q = $ this-> db-> get();
if($ q-> num_rows()> 0)
{
foreach($ q-> result()as $ row)
{
$ data [] = $ row;
}
return $ data;
}
else
{
show_404();
}
}
我甚至重复菜单
< ul class =profile_menu>
<?php foreach($ results as $ res):?>
< li>< a href =#class =profile_info>信息< / a>< / li>
< li>< a href =#class =profile_event>Eseményei(<?php echo count($ res-> title)?>)< / a& / li>
< li>< a href =#class =profile_photos>派对képei(0)< / a>< / li&
< li>< a href =#class =profile_video>Videói(<?php echo count($ res-> url);?>< / a& / li>
<?php endforeach;?>
谢谢
由于您缺少group by语句,因此您将有重复的内容是正常的。在您的情况下,您需要添加:
$ this-> db-> group_by(pf_users.uid);
现在你既有pf_videos又有pf_events,正常情况是有重复的内容,你有两个关于pf_events和pf_videos的信息,如果你不需要这两个信息,一个简单的DISTINCT为你做这项工作,所以你可以简单地拥有
$ this-> db-> select('DISTINCT pf_users。 *',false);
要了解有关DISTINCT和GROUP BY的详情,您只需阅读以下链接:
http://dev.mysql.com/doc/refman/5.0/en/distinct-optimization.html
http:// dev .mysql.com / doc / refman / 5.0 / en / group-by-functions.html
Okay first sorry if its a stupid question but im a really big beginner.
I have 3 database tables, videos, events, and users.
My problem is if i join these 3 tables and showing the results in my view it duplicates.
For example if i echo out the users name in my profile , and i uploaded 7 videos it shows the name seven times.
function get_profile($id)
{
$this->db->select('*');
$this->db->from('pf_users');
$this->db->join('pf_videos', 'pf_videos.uid = pf_users.uid');
$this->db->join('pf_events', 'pf_events.uid = pf_users.uid');
$this->db->where('pf_users.uid', $id);
$q = $this->db->get();
if($q->num_rows() > 0)
{
foreach($q->result() as $row)
{
$data[] = $row;
}
return $data;
}
else
{
show_404();
}
}
i even repeats the menus
<ul class="profile_menu">
<?php foreach ($results as $res): ?>
<li><a href="#" class="profile_info">Információ</a></li>
<li><a href="#" class="profile_event">Eseményei (<?php echo count($res->title) ?>)</a></li>
<li><a href="#" class="profile_photos">Party képei (0)</a></li>
<li><a href="#" class="profile_video">Videói (<?php echo count($res->url); ?></a></li>
<?php endforeach; ?>
could please someone point out what i am missing? and sorry if its a stupid question.
thank you
It is normal that you will have duplicate content as you are missing the group by statement. In your situation you need to add:
$this->db->group_by("pf_users.uid");
Now you have both joint pf_videos and pf_events and the normal thing is to have duplicate content as you have both information about pf_events and pf_videos . If you though don't need both information a simple DISTINCT will do the job for you, so you can simply have
$this->db->select('DISTINCT pf_users.*',false);
without using the group_by statement. To learn more about DISTINCT and GROUP BY you can simply read the following links:
http://dev.mysql.com/doc/refman/5.0/en/distinct-optimization.html
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html
这篇关于Codeigniter join重复返回的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!