【问题标题】:Using Inner Join in sqlite not returning the joined table's data在 sqlite 中使用内部联接不返回联接表的数据
【发布时间】:2014-07-23 05:28:35
【问题描述】:

我有一些药物想要获取它们的本地化名称,但在获取它们的本地化名称时遇到了问题(它们在返回中是空白的)。

这是查询(在运行时)。

SELECT m.Id as Id, m.Name as Name, lm.Name as ProductName 
FROM Medicine m 
INNER JOIN LocalizedMedicine lm 
    ON lm.MeId = m.Id 
    AND lm.LanguageCode = 'en-US' 
ORDER BY m.Name

这是我的课程。

public class LocalizedMedicine
{        
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }
    [Indexed("LocMedName", 0)]
    public string Name { get; set; }
    public string LanguageCode { get; set; }
    public int MeId { get; set; }
}



public class Medicine : ObservableObject
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }
    /// <summary>
    /// The universal Latin medical name.
    /// </summary>
    public string Name { get; set; }

    /// <summary>
    /// The primary Gene that this medicine uses.
    /// </summary>
    public int GeneId { get; set; }

    [Ignore]
    public Gene GeneUsed { get; set; }

    private string productName;
    /// <summary>
    /// The localized product name. It retrieves the
    /// localized name from AppResources if it is null.
    /// </summary>
    [Ignore]
    public string ProductName
    {
        get
        {
            // old attempt
            //if (productName == null)
            //    productName = AppResources.ResourceManager.GetString(Name);

            return productName;
        }
        set
        {
            productName = value;
            NotifyPropertyChanged("ProductName");
        }
    }

    /// <summary>
    /// Does a reference comparison and Name comparison, since the names (latin names)
    /// should be unique.
    /// </summary>
    /// <param name="m"></param>
    /// <returns></returns>
    public override bool Equals(object m)
    {
        if (m == null) return false;
        if (m == this) return true;
        if (((Medicine)m).Name == this.Name) return true;
        return false;
    }
}

这是我拨打电话的方式(在 PCL 中)

public IEnumerable<Medicine> GetAllMedicines(CultureInfo cultureInfo)
    {
        string langCode = cultureInfo.Name;

        // get all of the medicines and join LocalizedMedicine on MeId = med.Id
        // and LanguageCode = langCode


        var query = database.QueryAsync<Medicine>(
            "SELECT m.Id as Id, m.Name as Name, lm.Name as ProductName " +
            "FROM Medicine m " +
            "INNER JOIN LocalizedMedicine lm ON lm.MeId = m.Id AND lm.LanguageCode = " + "'" + langCode + "' " +
            "ORDER BY m.Name"
            );
        query.Wait();

        var result = query.Result;
        return result;
    }

【问题讨论】:

  • lm.LanguageCode = 'en-US' 不应该在WHERE 子句中吗?我不确定这是否会有所作为,但这样会更正确。
  • @jmcilhinney 它只会对外部连接产生影响。无论如何,当操作可以被描述为“加入英文翻译”时,这可以说是更正确的。
  • 查询看起来没问题。 langCode 变量中的确切值是多少? LanguageCode 列中的值是什么?如果手动执行,查询是否有效?
  • 语言代码列中的当前值为“en-US”(不带引号)。回家后我会进行更多故障排除,并尝试将 ON 更改为 WHERE。
  • 嗯,我把查询改成了WHERE,还是没有得到任何结果。在 SQLite 浏览器中运行查询,得到了我的预期。然后我查看了 Medicine 类,发现我在 Product Name 上有了 ignore 属性。嗯。哈哈 谢谢各位!我没有用“on”测试查询,所以我不知道这是否可行,但非常感谢你们!

标签: c# sqlite inner-join


【解决方案1】:

将查询条件从“ON”更改为“WHERE”,并从 Medicine 类中删除了忽略属性。哎呀。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-08
    • 2020-05-30
    • 1970-01-01
    • 2021-08-22
    • 1970-01-01
    相关资源
    最近更新 更多