Codeigniter:从3个表中选择不同的列

时间:2016-05-22 10:07:51

标签: codeigniter mysqli

今天我在开发我的应用时遇到了问题。 我重组了我的数据库。

我有3张表格,包括帖子,照片和帖子的标签。

我加入了另外两张桌子(照片,标签)。

我想查询前3个帖子,只有一个带有照片和标签的SELECT,但只显示结果中显示的第一行。

Heres是我的代码:

$this->db->select('owner, title, time, LEFT(content, 75) as content, category, url, price, county');

$this->db->from(POSTS_TABLE);

$this->db->join(PHOTOS_TABLE, PHOTOS_TABLE.'.post_id = '.POSTS_TABLE.'.url');

$this->db->join(TAGS_TABLE, TAGS_TABLE.'.post_url = '.POSTS_TABLE.'.url');

$this->db->order_by('time', 'desc');

$this->db->limit(3);

$query = $this->db->get();

$posts = $query->result();

我的数据库结构:

帖子表:

id, owner, title, content, url

标签表:id,post_id(与posts表中的url列相同),photo(照片链接)

标签表:

 id, post_url (same as the url column from the posts table), tag.

请帮忙。如果只有一个SELECT并显示正确的结果,我怎样才能实现这一目标?

我不能使用WHERE子句,因为我不知道确切的post_url

1 个答案:

答案 0 :(得分:0)

考虑列名称' url'是行的id

尝试:

// Code before
$this->db->join('PHOTOS_TABLE', 'PHOTOS_TABLE.post_id = POSTS_TABLE.url');
$this->db->join('TAGS_TABLE', 'TAGS_TABLE.post_url = POSTS_TABLE.url');
// Code After