Php 如何多次连接同一个表以检索不同的结果
我需要从同一时间请求记录,但不知道如何将它们彼此分离以避免合并 有一个表Php 如何多次连接同一个表以检索不同的结果,php,mysql,codeigniter,activerecord,Php,Mysql,Codeigniter,Activerecord,我需要从同一时间请求记录,但不知道如何将它们彼此分离以避免合并 有一个表categories,其中列CID,表示用于分离类别的组件ID: id cid title 1 1 News 2 1 Events 3 2 Users 4 2 Administrators 5 3 Guests 6 3 Registered 这是一个组件表: id title 1 Content 2 Me
categories
,其中列CID
,表示用于分离类别的组件ID:
id cid title
1 1 News
2 1 Events
3 2 Users
4 2 Administrators
5 3 Guests
6 3 Registered
这是一个组件表:
id title
1 Content
2 Members
3 Access level
我需要从数据库中检索结果,以便在我的content\u list\u view.php中打印这些变量:
<?php foreach($results as $item):?>
ID: <?php echo $item->id;?> <br/>
Title: <?php echo $item->title;?> <br/>
Category: <?php echo $item->category;?> <br/>
Access: <?php echo $item->access;?> <br/>
<?php endforeach;?>
public function get_content_list(){
$this->db
->select('
content.id,
content.title,
categories.title AS category,
categories.title AS access
')
->join('categories', 'content.catid = categories.id')
->join('categories AS alvl', 'content.alvl = alvl.id');
$query = $this->db->get($this->_table_name);
if($query->num_rows() > 0)
return $query->result();
else
return FALSE;
}
毕竟,在我看来,这就是我得到的,类别和访问是相同的。应注册访问权限:
ID: 1
Title: Some article test news
Category: News
Access: News
加入表时,有没有关于如何分离记录的建议?问题在于选择,因为您没有分配alvl
,并且总是从类别中进行分配
尝试更改此选项:
$this->db
->select('
content.id,
content.title,
categories.title AS category,
categories.title AS access
')
->join('categories', 'content.catid = categories.id')
->join('categories AS alvl', 'content.alvl = alvl.id');
为此:
$this->db
->select('
content.id,
content.title,
categories.title AS category,
alvl.title AS access
')
->join('categories', 'content.catid = categories.id')
->join('categories AS alvl', 'content.alvl = alvl.id');
问题出在select中,因为您没有分配alvl
,它总是从类别中获取
尝试更改此选项:
$this->db
->select('
content.id,
content.title,
categories.title AS category,
categories.title AS access
')
->join('categories', 'content.catid = categories.id')
->join('categories AS alvl', 'content.alvl = alvl.id');
为此:
$this->db
->select('
content.id,
content.title,
categories.title AS category,
alvl.title AS access
')
->join('categories', 'content.catid = categories.id')
->join('categories AS alvl', 'content.alvl = alvl.id');
哇!我甚至不知道我能用那种方式做到。非常感谢AlessandroGlad帮助你们,我通过了这个问题,第一次不太清楚如何做@aspirinemagawow!我甚至不知道我能用那种方式做到。非常感谢AlessandroGlad帮助你们,我对这个问题不感兴趣,第一次不太清楚如何做@aspirinemaga