[英]SQL - Finding all instances of a number in a table in one column and displaying data from another column in that row
I have a database with id numbers from 1 all the way to around 5000. Each page displays data from it's corresponding row based on page hashes using ajax to load the relevant data into each div.我有一个 id 编号从 1 一直到大约 5000 的数据库。每个页面显示来自其相应行的数据,基于页面哈希,使用 ajax 将相关数据加载到每个 div 中。 Each row has a varchar column that tells what other items are relevant.
每行都有一个 varchar 列,用于说明其他哪些项目是相关的。 On each page, I would like to cross index the whole table to find instances of the relevant id in other entries and display the title of that entry from the table with a link (which will be generated automatically using a script).
在每一页上,我想对整个表进行交叉索引以在其他条目中查找相关 id 的实例,并通过链接显示表中该条目的标题(将使用脚本自动生成)。 For example, if my id is 346, I would like to parse the 'relevant' column for any entries that contain a reference to #346 and display the data from the 'title' column of that row for each instance found in the table.
例如,如果我的 id 是 346,我想为包含对 #346 的引用的任何条目解析“相关”列,并为表中找到的每个实例显示该行的“标题”列中的数据。
So, a selection of my database looks like this.所以,我的数据库的选择看起来像这样。
+-------------------+----------------+------------------+------------------+
| id (INT) | title (VCHAR) | author (VCHAR) | relevant (VCHAR) |
+-------------------+----------------+------------------+------------------+
| 346 | title A | name | See #2 and #24 |
| 347 | title B | name | See #789 |
| 348 | title C | name | See #1 and #346 |
| 349 | title D | name | See #34 and #224 |
| 340 | title E | name | See #34 and #346 |
| 341 | title F | name | See #13 and #87 |
| 342 | title G | name | See #346 and #600|
| 343 | title H | name | See #346 and #808|
--------------------------------------------------------------------------
So, based on my url hash, which would be #346, I should see output into a div that looks like this所以,基于我的 url hash,这将是#346,我应该看到 output 进入一个看起来像这样的 div
See also:
Title C Title E Title G Title H
What is the best method for doing this?这样做的最佳方法是什么? Would it be better to use some other reference besides a url hash since I'm already pulling the id from the database using it?
除了 url hash 之外,使用其他参考会更好吗,因为我已经使用它从数据库中提取了 id?
As mentionned in the comment by @M.Eriksson, your database structure is wrong and will lead you to bad performances.正如@M.Eriksson 在评论中提到的那样,您的数据库结构是错误的,会导致您的表现不佳。
Now, if you need to keep your current data structure and if performances are not critical, you can sort this out by doing one more query for each results:现在,如果您需要保留当前的数据结构并且性能不重要,您可以通过对每个结果再执行一次查询来解决这个问题:
Loop over each of your articles, use a regex to extract all the #id of column relevant into an array.遍历您的每篇文章,使用正则表达式将所有相关列的#id 提取到数组中。
Build a SQL query based on that array:基于该数组构建一个 SQL 查询:
SELECT * FROM your_table WHERE id=id1 OR id=id2 OR id=id3... SELECT * 来自 your_table WHERE id=id1 OR id=id2 OR id=id3...
(that can be easily done using the function implode
) (可以使用 function
implode
轻松完成)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.