简体   繁体   English

从许多表中获取信息

[英]Getting info from many tables

I have 4 tables: question_tags, tags, questions and users. 我有4个表格:question_tags,标签,问题和用户。

Question structure is: q_id, q_title, q_content, q_date, q_author (id from users table). 问题结构为:q_id,q_title,q_content,q_date,q_author(来自用户表的ID)。

Tags structure is: tag_id, tag_name, tag_description. 标签结构为:tag_id,tag_name,tag_description。

Question_tags structure is: id, tag_id, q_id. Question_tags结构为:id,tag_id,q_id。

I want ot list all questions and next to every question title to show and its tags. 我要列出所有问题,并在要显示的每个问题标题及其标签旁边列出。 I've come up with this so far: 到目前为止,我已经提出了:

$this->db->join('users', 'q_author = users.id', 'left');
$this->db->order_by('q_id', 'desc');
$this->db->limit($per_page, $offset);
$query = $this->db->get('questions');

But have no ideas about the tags. 但是对标签一无所知。 (also I'm using CodeIgniter) (我也在使用CodeIgniter)

PS Every question can has more than one tag. PS每个问题可以有多个标签。

database table tags codeigniter-2 数据库表标签codeigniter-2

You'll need to have different queries if you want to avoid returning duplicate data in your main query. 如果要避免在主查询中返回重复数据,则需要使用其他查询。 Because you have many tags related to each question you should loop through your question results and within each loop iteration query the tags for that question. 因为您有许多与每个问题相关的标签,所以您应该循环浏览问题的结果,并在每次循环迭代中查询该问题的标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM