如何在SQLite中查找一个列表是否包含另一个列表的任何元素

huangapple go评论77阅读模式
英文:

How to find if a list contains any element of another list in sqlite

问题

我有一个 sqlite 表,其中有一列名为 selected_dates。此列包含了一个 CustomDates 对象的列表。我想查询这一列是否包含另一个相同类型的列表(customDates)中的任何项。但是我找不到任何方法。我尝试过:

    @Query("SELECT * FROM schedule_table WHERE (type_daily IS 1) OR (:monthOfYear IS 1) " +
            "OR (selectedDates IN (:customDates))" +
            "ORDER BY scheduleID DESC")
    LiveData<List<Schedule>> getSchedulesOfThisWeek(String monthOfYear, List<ScheduleType.Dates> customDates);

这是我的示例数据(List<Date> 对象以 json 格式存储)sqlite browser 中的数据图像

[{ "dayOfMonth":1, "month":8 },{ "dayOfMonth":2, "month":8 },{ "dayOfMonth":3, "month":8 },{ "dayOfMonth":4, "month":8 },{ "dayOfMonth":5, "month":8 },{ "dayOfMonth":6, "month":8 },{ "dayOfMonth":7, "month":8 },{ "dayOfMonth":8, "month":8 },{ "dayOfMonth":9, "month":8 },{ "dayOfMonth":10, "month":8 },{ "dayOfMonth":11, "month":8 },{ "dayOfMonth":12, "month":8 },{ "dayOfMonth":13, "month":8 },{ "dayOfMonth":14, "month":8 },{ "dayOfMonth":15, "month":8 },{ "dayOfMonth":16, "month":8 },{ "dayOfMonth":17, "month":8 },{ "dayOfMonth":18, "month":8 },{ "dayOfMonth":19, "month":8 },{ "dayOfMonth":20, "month":8 },{ "dayOfMonth":21, "month":8 },{ "dayOfMonth":22, "month":8 },{ "dayOfMonth":23, "month":8 },{ "dayOfMonth":24, "month":8 },{ "dayOfMonth":25, "month":8 },{ "dayOfMonth":26, "month":8 },{ "dayOfMonth":27, "month":8 },{ "dayOfMonth":28, "month":8 },{ "dayOfMonth":29, "month":8 }]

简而言之,在 sqlite 中如何找出两个列表是否相交?

英文:

I have a sqlite table where there is a column selected_dates. This column contains a list of CustomDates object. I want to query if this column contains any item of other list of same type( customDates). But I can't find any way. I've tried:

    @Query(&quot;SELECT * FROM schedule_table WHERE (type_daily IS 1) OR (:monthOfYear IS 1) &quot; +
            &quot;OR (selectedDates IN (:customDates))&quot; +
            &quot;ORDER BY scheduleID DESC&quot;)
    LiveData&lt;List&lt;Schedule&gt;&gt; getSchedulesOfThisWeek(String monthOfYear, List&lt;ScheduleType.Dates&gt; customDates);

Here is my sample data (List&lt;Date&gt; object is stored in json format) Image of data in sqlite browser:

[{&quot;dayOfMonth&quot;:1,&quot;month&quot;:8},{&quot;dayOfMonth&quot;:2,&quot;month&quot;:8},{&quot;dayOfMonth&quot;:3,&quot;month&quot;:8},{&quot;dayOfMonth&quot;:4,&quot;month&quot;:8},{&quot;dayOfMonth&quot;:5,&quot;month&quot;:8},{&quot;dayOfMonth&quot;:6,&quot;month&quot;:8},{&quot;dayOfMonth&quot;:7,&quot;month&quot;:8},{&quot;dayOfMonth&quot;:8,&quot;month&quot;:8},{&quot;dayOfMonth&quot;:9,&quot;month&quot;:8},{&quot;dayOfMonth&quot;:10,&quot;month&quot;:8},{&quot;dayOfMonth&quot;:11,&quot;month&quot;:8},{&quot;dayOfMonth&quot;:12,&quot;month&quot;:8},{&quot;dayOfMonth&quot;:13,&quot;month&quot;:8},{&quot;dayOfMonth&quot;:14,&quot;month&quot;:8},{&quot;dayOfMonth&quot;:15,&quot;month&quot;:8},{&quot;dayOfMonth&quot;:16,&quot;month&quot;:8},{&quot;dayOfMonth&quot;:17,&quot;month&quot;:8},{&quot;dayOfMonth&quot;:18,&quot;month&quot;:8},{&quot;dayOfMonth&quot;:19,&quot;month&quot;:8},{&quot;dayOfMonth&quot;:20,&quot;month&quot;:8},{&quot;dayOfMonth&quot;:21,&quot;month&quot;:8},{&quot;dayOfMonth&quot;:22,&quot;month&quot;:8},{&quot;dayOfMonth&quot;:23,&quot;month&quot;:8},{&quot;dayOfMonth&quot;:24,&quot;month&quot;:8},{&quot;dayOfMonth&quot;:25,&quot;month&quot;:8},{&quot;dayOfMonth&quot;:26,&quot;month&quot;:8},{&quot;dayOfMonth&quot;:27,&quot;month&quot;:8},{&quot;dayOfMonth&quot;:28,&quot;month&quot;:8},{&quot;dayOfMonth&quot;:29,&quot;month&quot;:8}]

In a word, How to find if two List Intersects each other or not in sqlite?

答案1

得分: 1

基本上,Room Database以基本数据类型存储数据。因此,您选择的日期以TEXT格式存储。您可以执行LIKE操作以查找匹配的列。

Room还提供了一个注释@RawQuery。您可以使用这个。对于您的问题,这可能是一个解决方案:

Dao接口中:

@RawQuery(observedEntities = Schedule.class)
LiveData<List<Schedule>> getSchedule(SupportSQLiteQuery query);

并且使用getSchedule()方法:

String query = "SELECT * FROM schedule_table WHERE " +
                "(type_daily IS 1) OR (" + monthOfYear + " IS 1) OR " +
                conditions + " ORDER BY scheduleID DESC";
return mDao.getSchedule(new SimpleSQLiteQuery(query));

其中condition是您想要查找的日期的String。例如:

String condition = "selectedDates LIKE '%{\"dayOfMonth\":1,\"month\":8}%' OR selectedDates LIKE '%{\"dayOfMonth\":5,\"month\":8}%';

您可以创建一个基于List<ScheduleType.Dates>生成此字符串的方法。

英文:

Basically Room Database Stores data in primary data types. So your selected dates are in TEXT format. You can perform LIKE operation to find the matching columns.

Room also provides a annotation @RawQuery. You can use this. For your problem this might be a solution:

in Dao interface:

@RawQuery(observedEntities = Schedule.class)
LiveData&lt;List&lt;Schedule&gt;&gt; getSchedule(SupportSQLiteQuery query);

And to use getSchedule():

String query = &quot;SELECT * FROM schedule_table WHERE &quot; +
                &quot;(type_daily IS 1) OR (&quot; + monthOfYear + &quot; IS 1) OR &quot; +
                conditions + &quot; ORDER BY scheduleID DESC&quot;;
        return mDao.getSchedule(new SimpleSQLiteQuery(query));

where condition is the String of the dates you want to find. For example:

String condition = &quot;selectedDates LIKE &#39;%{\&quot;dayOfMonth\&quot;:1,\&quot;month\&quot;:8}%&#39; OR selectedDates LIKE &#39;%{\&quot;dayOfMonth\&quot;:5,\&quot;month\&quot;:8}%&#39;;

You can create a method which will generate this string based on a List&lt;ScheduleType.Dates&gt;.

huangapple
  • 本文由 发表于 2020年9月29日 21:44:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/64120919.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定