简体   繁体   English

将一个表中的列与另一个表进行匹配

[英]Matching columns from one table with another

Sorry for the confusing title, let me try to explain. 抱歉,标题令人困惑,让我尝试解释一下。 The question relates to custom logging software where a company can design their own 'log'. 问题与定制日志软件有关,公司可以在其中设计自己的“日志”。 Each log has a corresponding metadata table that has the columns from their log in it. 每个日志都有一个对应的元数据表,其中包含其日志中的列。

I am using php to render out a display of the data in the log. 我正在使用php渲染日志中的数据显示。 I first access the column names in the meta table and use that to build the table headers with a foreach as shown 我首先访问元表中的列名,并使用它通过一个foreach构建表头,如下所示

foreach ($logStructure as $header)
{
   echo '<th>' . $header['label'] . '</th>';
}
endforeach;  

Now I have done another select from the actual log for the rows to display and have that in an array call $logrow. 现在,我从实际日志中进行了另一个选择,以显示要显示的行,并将其放在数组中,调用$ logrow。 How do I go about iterating through those rows and putting the right data in the right column? 如何遍历这些行并将正确的数据放在正确的列中? In SQl I would have used a WHERE Clause to say something to the equivalent of the following: 在SQl中,我将使用WHERE子句来表达以下内容:

'WHERE $logrow['fieldname'] = $header['label']'

As an example the metatable fields look like this 举例来说,元表字段如下所示

[metaid] [columnLabel] [columnNameInData]
   001     Log Number       logNum
   002       Author        CreateUser
   003       Subject        Entry

The data gets put into the $header[] via a statement like such 数据通过这样的语句放入$ header []中

SELECT columnLabel AS label FROM metatable;

The Log Structure Looks Like This 日志结构看起来像这样

[logNum]  [CreateUser]       [Entry]
   001       Admin       Some Text about Stuff
   002      Editor 1     Another Amazing Entry
   003      Editor 2      It gets old by now

So the select looks like this 所以选择看起来像这样

SELECT logNum, CreateUser, Entry FROM log 

The data gets put into $logrow['lognum'], $logrow['CreateUser'], $logrow['Entry'] 数据放入$logrow['lognum'], $logrow['CreateUser'], $logrow['Entry']

Hopefully this clears the question up a little better, I'm looking to match the column names in metadata with the values from those fields in the actual data. 希望这可以更好地解决问题,我希望将元数据中的列名与实际数据中这些字段的值相匹配。

If the sequence is gonna be always the same, and you are only trying to render it in the same table, I don't see what's stopping you to read both out into 2 separate arrays, then plot it onto the table. 如果序列总是相同的,而您只想在同一张表中呈现它,我看不出是什么使您无法将它们读入2个单独的数组,然后将其绘制到表上。

First this: 首先这个:

SELECT columnNameInData FROM metatable ORDER BY metaid;

put the result into an array $sort 将结果放入数组$sort

Then implode it and build your second select clause: 然后将其内爆并构建第二个select子句:

$columns=implode(', ', $sort);
$SQL="SELECT $columns FROM log";

And read the result into array $result2 并将结果读入数组$result2

Finally, assemble the header you have and the output of the $result2 as the rows and tds of logs, this table should have the records aligned with the headers. 最后,将您拥有的标题和$ result2的输出组装为日志的行和tds,此表应使记录与标题对齐。

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

相关问题 使用LIKE将数据从一个表匹配到另一个表 - matching data from one table to another with LIKE 从一个表中选择匹配值到另一个表中的多个值 - selecting matching value from one table to multiple values in another 从两个表中各选择一个记录。 通过将另一个值与新表匹配来更新一个值 - select one record each from two table. update the one value by matching the another one to a new table 从一个表到另一列的不同表中检索数据 - Retrieving Data From One Table To Another Table With Different Columns SQL将行从一个表移动到另一个表,具有不匹配的列 - SQL move row from one table to another with unmatched columns 如何将一个表中的两个或更多列与另一个表中的一个列联接在一起,即使第一个表列中的值为NULL - How to join two or more columns from one table with one column from another table, even there are NULL values in first table columns 如何通过将一个表中的 id 与另一个表匹配来选择和更新一个表中的记录? - How to select and update records in one table by matching ids from it with another table? 从Zend中的表中选择完全匹配的列...? - Select exact matching columns from table in Zend…? 计数一个记录表以匹配另一个表的记录 - Counting one table of records for matching records of another table 匹配SQL列并将其解析为另一个SQL表 - Matching SQL columns and parsing them into another SQL table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM