【问题标题】:SQL query to format table data for DataSource in GridView用于格式化 GridView 中 DataSource 的表数据的 SQL 查询
【发布时间】:2016-08-18 20:11:49
【问题描述】:

我正在寻找可以传输源 SQL 表数据的 SQL Server 查询:

TextID | Text  | LanguageID
-------|-------|-------------------------------------
app.aa | Hi    | 6a13ea09-46ea-4c93-9b6a-e26bdc6ff4d8
app.cc | Hund  | 0c894bb7-4937-4903-906a-d1b1dd64935c
app.aa | Hallo | 0c894bb7-4937-4903-906a-d1b1dd64935c
app.cc | Dog   | 6a13ea09-46ea-4c93-9b6a-e26bdc6ff4d8
app.bb | Star  | 6a13ea09-46ea-4c93-9b6a-e26bdc6ff4d8
... 

进入这样的表:

TextID | Original | Translated
-------|----------|-----------
app.aa | Hi       | Hallo
app.bb | Star     | -
app.cc | Dog      | Hund
...

这样我就可以将它用作 ASP .NET 中 GridView 的数据源。提前感谢您的帮助。

【问题讨论】:

    标签: sql asp.net database gridview formatting


    【解决方案1】:

    当您需要将两个不同行的数据合并为一个时,您需要加入。例如:

    select src.TextID "TextID", src.Text "Original", tr.Text "Translated"
    from source_table src
    left join source_table tr
    on src.TextID = tr.TextID
    and src.LangID = 'xxx'  -- xxx is the source language id
    and tr.LangID = 'yyy' -- yyy is the target language id
    

    左连接确保未翻译的单词包含在空翻译值中。要为您的数据源创建一个表,您需要将 create table(或者可能是 create view)包裹在 select 周围:

    create table translations as
        select ...
    

    【讨论】:

    • 非常感谢您的帮助。查询就像一个魅力,但 LEFT JOIN 非常慢,因为数据库中有很多条目。有什么方法可以替换它,例如使用 INNER JOIN 或任何其他替代方法使其更快?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-31
    • 2021-07-08
    • 2016-12-12
    • 1970-01-01
    • 2020-09-15
    • 2023-03-12
    • 1970-01-01
    相关资源
    最近更新 更多