简体   繁体   English

在两个表查询(SQL和ColdFusion)中匹配值

[英]Matching values in two tables query (SQL and ColdFusion)

I have a query that has a list of base values and a list of language values. 我有一个查询,其中包含基本值列表和语言值列表。 Each value has a key that matches to the other. 每个值都有一个相互匹配的键。 The base values are stored in one table and the language values in another. 基本值存储在一个表中,语言值存储在另一个表中。 My problem is that I need to get all matching base values removed from the QUERY except for one. 我的问题是我需要从QUERY中删除所有匹配的基本值,但其中一个除外。 Then, I export that query into an Excel spreadsheet (I can do this portion fine) and allow the user to edit the language values. 然后,我将该查询导出到Excel电子表格中(我可以做得很好),并允许用户编辑语言值。

When the user edits and/or inserts new language values, I need to update the database except now writing over any matching values in the database (like those that were removed the first time). 当用户编辑和/或插入新的语言值时,除了现在要覆盖数据库中所有匹配的值(例如第一次删除的那些值)外,我需要更新数据库。

In simplicity, the client pays for translations and if I can generate a sheet that has fewer translations needed (like phrases that reappear often) then they can save money, hence the project to begin with. 简单来说,客户支付翻译费用,如果我可以生成所需翻译次数较少的工作表(例如经常出现的短语),那么他们可以节省资金,因此项目就从此开始。 I realize the downside is that it is not a true linked list, where all matching values all belong to one row in the language table (which would have been easy). 我意识到缺点是它不是一个真正的链表,其中所有匹配的值都属于语言表中的一行(这很容易)。 Instead, there are multiple values that are identical that need to be updated as described above. 相反,有多个相同的值需要如上所述进行更新。


Yeah, I'm confused on it which is why it might seem a little vague. 是的,我对此感到困惑,这就是为什么它看起来有点模糊的原因。 Here's a sample: 这是一个示例:

Table 1
Item Description1
Item Description2
Item Description3
Item Description2
Item Description2
Item Description4
Item Description5
Item Description6
Item Description3

Table 2
Item Desc in other Language1
Item Desc in other Language2
Item Desc in other Language3  (blank)
Item Desc in other Language3
Item Desc in other Language4
Item Desc in other Language5
*blank*

Desired Result (when queried) 所需结果(查询时)

Table 1 Item Description1 Item Description2 Item Description3 Item Description4 Item Description5 Item Description6 表1项目说明1项目说明2项目说明3项目说明4项目说明5项目说明6

Table 2
Item Desc in other Language1
Item Desc in other Language2
Item Desc in other Language3 (filled by matching row in Table 2)
Item Desc in other Language4
Item Desc in other Language5
Item Desc in other Language6 (blank, returned as empty string)

The user makes their modifications, including inserting data into blank rows (like row 6 for the language) then reuploads: 用户进行修改,包括将数据插入空白行(如该语言的第6行),然后重新上传:

Table 1
Item Description1
Item Description2
Item Description3
Item Description2
Item Description2
Item Description4
Item Description5
Item Description6
Item Description3

Table 2
Item Desc in other Language1
Item Desc in other Language2
Item Desc in other Language3  (now matches row below)
Item Desc in other Language3
Item Desc in other Language4
Item Desc in other Language5
Item Desc in other Language6  (new value entered by user)

There is also a resource key that matches each "Item Description" to a single "Item Desc in other Language". 还有一个资源密钥,将每个“项目描述”与单个“其他语言的项目描述”相匹配。 The only time they are ever going to see each other is during this translation process, all other times the values may be different, so the resource keys can't simply be changed to all point at one translation permanently. 他们唯一一次见面是在此翻译过程中,其他所有时间值可能都不同,因此不能简单地将资源密钥永久更改为一次翻译中的所有内容。

I should also add, there should be no alteration of the structure of the tables or removing rows of the table. 我还应该补充一点,不应更改表的结构或删除表的行。


Ok, here's an updated revisal of what I would LIKE the query to do, but obviously does not do since I actually need the values of the joined table: 好的,这是我喜欢查询要执行的操作的最新修订,但是显然不这样做,因为我实际上需要联接表的值:

SELECT pe.prodtree_element_name_l, rs.resource_value, pe.prodtree_element_name_l_rk   
FROM prodtree_element pe
        LEFT JOIN resource_shortstrings rs
            ON pe.prodtree_element_name_l_rk = rs.resource_key
        WHERE rs.language_id = '5'
            AND pe.prodtree_element_name_l <> ''
        GROUP BY pe.prodtree_element_name_l

Hrm, still not real clear on what the issue truly is, but let me give it a go. 嗯,对于这个问题到底是什么,仍然不清楚,但是让我试一下。

Tables: 表格:

BASE_VALUES
------------------
BASE_VALUE_RK
BASE_VALUE_NAME

RESOURCE_VALUES (these are the translations, I'm guessing)
-----------------------
RESOURCE_KEY
RESOURCE_LANGUAGE_ID
RESOURCE_VALUE

You want to retrieve one base value, and all corresponding translation values that match that base value, export them to excel, and then re-load them via an update (I think). 您想要检索一个基本值,以及与该基本值匹配的所有相应转换值,将它们导出到excel,然后通过更新重新加载它们(我认为)。

SQL to SELECT out data: SQL选择数据:

SELECT bv.BASE_VALUE_RK, rv.RESOURVE_VALUE
  FROM BASE_VALUE bv, RESOURCE_VALUE rv
 WHERE bv.BASE_VALUE_RK = rv.RESOURCE_KEY
   AND rv.resource_language_id = '5'
 ORDER BY 1;

That'll give you: 那会给你:

1234    Foo
1235    Bar
1236    Baz

Export that to excel, and get it back with updates: 将其导出为ex​​cel,并通过更新将其恢复:

1234    Goo
1235    Car
1236    Spaz

You can then say: 然后您可以说:

UPDATE RESOURCE_VALUES
   SET RESOURCE_VALUE = value_from_spreadsheet
 WHERE RESOURCE_KEY = key_from_spreadsheet

I may be way off base on this guy, so let me know and, if you can provide a little more detail, I may be able to score closer to the mark. 我可能对这个人不太了解,所以让我知道,如果您能提供更多细节,我也许可以得分更高。

Cheers! 干杯!

If you need to remove all matches except for one, why not delete all the matching ... matches ... we need better terms ... and then insert the correct one. 如果您需要删除除一个以外的所有匹配项,为什么不删除所有匹配项……匹配项……我们需要更好的术语……然后插入正确的匹配项。 EG, if you need to update the matches between items 12 and 13 in the base pair table, do something like EG,如果您需要更新基本对表中项目12和13之间的匹配项,请执行以下操作

delete from matchtable where (id1 = 12 and id2 = 13) or (id1 = 13 and id2 = 13);
insert into matchtable (id1, id2) values (12, 13);

I may be oversimplifying, but your description seems vague in places. 我可能会过分简化,但是您的描述在某些地方似乎含糊不清。

Hey thanks for that update! 嘿,谢谢你的更新!

Looking at that and adding it into a previous post I finally came up with this: 看着它并将其添加到以前的帖子中,我终于想到了:

<cfquery name="getRows" datasource="XXXX">
    SELECT pe.prodtree_element_name_l, MAX(rs.resource_value) AS resource_value
    FROM prodtree_element pe
    LEFT JOIN resource_shortstrings rs
        ON pe.prodtree_element_name_l_rk = rs.resource_key
    WHERE rs.language_id = '5'
        AND pe.prodtree_element_name_l <> ''
    GROUP BY prodtree_element_name_l
</cfquery>

I realized I didn't need a specific resource_value, just any that was in there. 我意识到我不需要一个特定的resource_value,只需其中的任何一个。 I also realized I didn't need the resource key at all outside of the query. 我还意识到我根本不需要查询之外的资源密钥。 The update will be updating ALL the matching base values regardless, so I didn't really need the resource key after all, which allowed me to use the GROUP BY. 无论如何,此更新将更新所有匹配的基值,因此我毕竟并不需要真正的资源键,因此可以使用GROUP BY。

Took a while, sorry for the poor explanation, but this is it! 花了一段时间,对不起您的解释,抱歉! :) Thanks for all the help. :)感谢您的所有帮助。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM