通过PHP从Postgres有效地获取许多标签的问题 [英] To get many tags for a question effectively from Postgres by PHP

查看:117
本文介绍了通过PHP从Postgres有效地获取许多标签的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过PHP从 Postgres数据库有效地获取问题的多个标签?

How can you get many tags for a question effectively from Postgres database by PHP?

我有表格 问题,其中有变量 questions_question_id

I have tables tags and questions where I have the variables questions_question_id and question_id, respectively, for instance.

一个问题可能有许多标签。

One question may have many tags. I can select question_id, title and a tag from the tables. However, I do not know how to get many tags for one question out of the database effectively. PHP restricts me in manipulating the data as matrices.

我可以在变量$ result中读取question_id,title和tag。
数据是一个矩阵为我在variale $结果。
然而,我不能像矩阵一样引用数据。

I can read question_id, title and tag to the variable $result. The data is a matrix for me in the variale $result. However, I cannot refer to the data like to a matrix.

我只找到了 pg_fetch_row pg_fetch_all_columns 。前者将数据作为行数组,后者作为列数组。

I have only found the functions pg_fetch_row and pg_fetch_all_columns in PHP. The former gets the data as arrays for rows, while the latter as arrays for columns.

我的问题与 LEFT JOIN 建议我可能无法获得如下数据

My problem with LEFT JOIN suggests me that it may not be possible to get the data as follows


  1. 获取 title,question_id 标签的列 b $ b
  2. 按照question_id对数据进行分组,以便我可以将标签添加到我的问题:我已经尝试通过PHP操作数据,但我只获得 this far 。

  1. get the columns for title, question_id and tag
  2. group the data by question_id such that I can put tags to my questions: I have tried to manipulate the data by PHP, but I get only this far.


推荐答案

你想要的是第二个查询,获取给定问题的所有标签。试图加入他们在一起是很麻烦。它可以完成,但是你最终会得到几行具有相同问题数据但不同标签数据的行。

I think what you want is a second query that gets all the tags for a given question. Trying to join them together is gonna be cumbersome. It can be done but you'll end up with several rows with the same question data but different tag data.

因此,首先做这样的事情(我不知道您的数据库的结构,所以这只是一个例子):

So first do something like this (I don't know the structure of your database so this is just an example):

SELECT * FROM QUESTIONS WHERE ID = <id>;

然后,在该查询之后执行查询,询问所有标签:

Then you do a query right after that one, which asks for all the tags:

SELECT * FROM TAGS WHERE QUESTION_ID = <question_id>;

这篇关于通过PHP从Postgres有效地获取许多标签的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆