简体   繁体   English

SQL - 在表格的一列中查找数字的所有实例,并在该行的另一列中显示数据

[英]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.

相关问题 SQL - 将数据从一个表列复制到另一个表列 - SQL - copy data from one table column to another table column SQL复制数据从一列到另一个表指定列 - SQL copy data from one column to another table specified column 将数据从一个 sql 表复制到另一个具有较少列数和不同列的表 - copy data from one sql table to another having less number of columns and a different column 如何从表中获取所有数据,如果在同一列中重复相同的值,则该行应计数一次? - How to get get all the data from table and if same value repeated in one column that row should count once? PHP,HTML和MySqli-如何在一个表行中显示特定列的所有数据 - PHP, HTML and MySqli - how to show all data from specific column in one table row MySql中如何将列数据从一张表复制到另一张表? - How to Copy Column data from one table to another table in MySql? 根据行将数据从一列插入另一MYSQL - Insert data from one column to another MYSQL based on row 将一个表的行数分配给另一个表的列 - Assigning number of rows of one table to column of another 将数据从同一行中的一行复制到另一行中 - Copying data from one row to another in the same table SQL SQL 查询将值从一列添加到另一列作为新行一次 - SQL query add value from one column to another column as a new row just once
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM