SQL Server 2016过滤器内部json_value

时间:2016-05-16 04:08:09

标签: sql-server sql-server-2016

我有一个feeds表,其中包含喜欢它的json数组列(UserLike)。表格如下:

FeedID  FeedName    UserLike
 1       Feed 1      [{"UserID":1,"UserName":"User 1"},{"UserID":2,"UserName":"User 2"},...]
 2       Feed 2      [{"UserID":1,"UserName":"User 1"},{"UserID":2,"UserName":"User 2"},...]
 3       Feed 3      [{"UserID":1,"UserName":"User 1"}]

我希望通过比较UserID获取供稿列表并完全类似于用户登录信息(如果他喜欢,或者不返回UserLike null,即使登录用户不在{,我也希望获得供稿行{1}}列表)。

我该怎么办? T-SQL是否支持以下内容:

UserLike

我期待的结果是:

select 
    FeedID, FeedName, 
    Json_value(UserLike, '$[UserID=1].UserName')...
带有WHERE子句的

:UserID = 2

1 个答案:

答案 0 :(得分:0)

 <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:layout_gravity="bottom"
                app:tabMode="scrollable"
                app:tabGravity="center"
                app:tabIndicatorHeight="4dip"
                app:tabTextAppearance="@style/NavigationTabTextAppeareance"
                app:tabIndicatorColor="#ffffff" />

输出:

--Here we take all feeds
;WITH cte AS (
SELECT DISTINCT FeedID,
                FeedName
FROM dbo.feeds
--Here we take parsed JSON
), feeds AS (
SELECT  FeedID,
        FeedName,
        UserID,
        UserName
FROM [dbo].[feeds] f
CROSS APPLY OPENJSON ([UserLike]) 
WITH (
    UserID int,
    UserName nvarchar(255)
))
--And here we join them
SELECT c.FeedID,
        c.FeedName,
        f.UserID,
        f.UserName
FROM cte c
LEFT JOIN feeds f
    ON f.FeedID = c.FeedID and f.UserID = 2