从许多表中获取信息
我有 4 个表:question_tags、标签、问题和用户。
问题结构为:q_id, q_title, q_content, q_date, q_author (id 来自用户表)。
标签结构为:tag_id、tag_name、tag_description。
Question_tags结构为:id, tag_id, q_id。
我想列出所有问题以及要显示的每个问题标题及其标签。到目前为止我已经想出了这个:
$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');
但对标签没有任何想法。 (我也在使用 CodeIgniter)
PS 每个问题都可以有多个标签。
数据库表标签 codeigniter-2
I have 4 tables: question_tags, tags, questions and users.
Question structure is: q_id, q_title, q_content, q_date, q_author (id
from users table).Tags structure is: tag_id, tag_name, tag_description.
Question_tags structure is: 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)
P.S. Every question can has more than one tag.
database table tags codeigniter-2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想避免在主查询中返回重复数据,则需要使用不同的查询。由于您有许多与每个问题相关的标签,因此您应该循环遍历问题结果,并在每次循环迭代中查询该问题的标签。
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.