【问题标题】:Is it possible to split value from a column to rows when query in SQLite?在 SQLite 中查询时是否可以将值从列拆分为行?
【发布时间】:2017-01-24 10:12:20
【问题描述】:

从下面的表 A 中,我想从列颜色中拆分值和 用它生成新行。我想要的结果显示在表结果中 下面。

A表

动物 |性别 |颜色
-------|--------|---------
 猫 |男 |白色、黑色、棕色
 猫 |女 |白色的
 狗 |男 |黑色, 黄色
 狗 |女 |黑色

表格结果

动物 |性别 |颜色
-------|--------|---------
 猫 |男 |白色的
 猫 |男 |黑色的
 猫 |男 |棕色的
 猫 |女 |白色的
 狗 |男 |黑色的
 狗 |男 |黄色的
 狗 |女 |黑色的

我如何使用 SQLite 实现这一点,或者还有其他建议吗?

【问题讨论】:

  • 显示您的代码以获得更好的帮助
  • @Vickyexpert 现在我完全不知道如何才能完成这项工作。到目前为止,我发现使用来自 here 的递归,但我不知道如何将它应用到我的案例中。
  • 最好在 SQL 之外完成。
  • @Krsnik195 检查我的答案并使用它,它会帮助你
  • @Vickyexpert 我现在无法尝试您的代码,但正如我所见,它应该可以正常工作。无论如何,我会先标记你的答案。谢谢。

标签: android sqlite


【解决方案1】:

假设您将每条记录作为字符串获取,正如我在下面提到的那样,您可以从那里拆分所有数据,如下所示:

创建一个包含动物、颜色和性别等所有属性的类,如下所示

  class AnimalData         // You can give any name 
  {
      String strAnimal, strGender, strColor;

      public AnimalData(String strAnimal, String strGender, String strColor)
      {
          this.strAnimal = strAnimal;
          this.strGender = strGender;
          this.strColor = strColor;
      }

      public String getStrAnimal()
      {
          return strAnimal;
      }

      public String getStrGender()
      {
          return strGender;
      }

      public String getStrColor()
      {
          return strColor;
      }
  }

现在我正在使用 3 个静态变量,其中包含您从数据库中获取的上述数据,您需要使用它们而不是下面的这些变量

    String strAnimalData[] = {"Cat","Cat","Dog","Dog"};
    String strGenderData[] = {"Male","Female","Male","Female"};
    String strColorData[] = {"White,Black,Brown","White","Black,Yellow","Black"};

现在我正在使用一种方法,它会给我从上述值列出的结果

public List<AnimalData> getAllAnimalData()
{
      List<AnimalData> strAllAnimalData = new ArrayList<>();

      for(int i = 0; i < strAnimalData.length; i++)
      {
          if(strAnimalData[i].indexOf(",") > 0)
          {
              String strSubAnimalData[] = strAnimalData[i].split(",");

              for(int j = 0; j < strSubAnimalData.length; j++)
              {
                  AnimalData ad = new AnimalData(strSubAnimalData[j], strGenderData[i], strColorData[i]);
                  strAllAnimalData.add(ad);
              }
          }
          else if(strGenderData[i].indexOf(",") > 0)
          {
              String strSubGenderData[] = strGenderData[i].split(",");

              for(int j = 0; j < strSubGenderData.length; j++)
              {
                  AnimalData ad = new AnimalData(strAnimalData[i], strSubGenderData[j], strColorData[i]);
                  strAllAnimalData.add(ad);
              }
          }
          else if(strColorData[i].indexOf(",") > 0)
          {
              String strSubColorData[] = strColorData[i].split(",");

              for(int j = 0; j < strSubColorData.length; j++)
              {
                  AnimalData ad = new AnimalData(strAnimalData[i], strGenderData[i], strSubColorData[j]);
                  strAllAnimalData.add(ad);
              }
          }
          else
          {
              AnimalData ad = new AnimalData(strAnimalData[i], strGenderData[i], strColorData[i]);
            strAllAnimalData.add(ad);
          }
      }

      // Below code is just for Display Data

      for(AnimalData tempData: strAllAnimalData)
      {
          System.out.println("\t" + tempData.getStrAnimal() + "\t" + tempData.getStrGender() + "\t" + tempData.getStrColor());
        System.out.println("\n");
    }
}

注意:- 目前,当每个位置只有一个变量有更多数据时,这将起作用,例如如果动物在位置 1 有“Cat,Dog”并且 Color 在位置也有“Red,Brown” 1 然后它只会按颜色而不是颜色来区分动物,因为你需要在 for 循环中实现相同的逻辑。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-17
    • 2021-10-04
    • 1970-01-01
    • 1970-01-01
    • 2020-11-26
    • 1970-01-01
    • 1970-01-01
    • 2015-12-14
    相关资源
    最近更新 更多