久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

組合子查詢中的行的 Select 語句(樞軸)

Select statement that combines rows in a subquery (pivot)(組合子查詢中的行的 Select 語句(樞軸))
本文介紹了組合子查詢中的行的 Select 語句(樞軸)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我有:

tbl_product:
------------
product_id
name
description

tbl_user:
------------
user_id
first_name
last_name
initials

多個用戶可以擁有一個產(chǎn)品,我通過創(chuàng)建一個表格來表示:

Multiple users can own a single product and I represent that by creating a table:

xref_product_user: 
product_id
user_id 

組成復(fù)合主鍵,其中每一列都是各自表的外鍵.

that make up a composite primary key where each column is a foreign_key to their respective tables.

由于每個產(chǎn)品可以有多個用戶,所以我需要寫一個包含

Since each product can have multiple users, I need to write a select statement that contains

產(chǎn)品名稱、描述、組合用戶首字母(逗號分隔的字符串).

product name, description, combined user initials (comma separated string).

假設(shè)我有一個產(chǎn)品 chocolate 歸用戶 mike 所有約翰遜丹威廉姆斯.那么我的結(jié)果應(yīng)該是

So lets say I have a product chocolate that are owned by user mike johnson and dan williams. Well my results should be

NAME        DESCRIPTION    INTIALS
chocolate   candy          mj, dw

由于首字母部分,我似乎無法弄清楚如何編寫此 select 語句.有人有什么想法嗎?

I can't seem to figure out how to write this select statement because of the initials part. Anyone have any ideas?

推薦答案

函數(shù)可能是一種很好的、??易于維護的處理方法:

A Function would probably be a good, easily maintainable way to handle that:

CREATE FUNCTION [dbo].[fn_GetInitialsForProduct]
(
    @product_id
)
RETURNS varchar(200)
AS
BEGIN
    declare @Initials varchar(200)

    set @Initials = ''

    select @Initials=@Initials + ', ' + isnull(u.Initials, '')
    from dbo.tbl_user u
    inner join dbo.xref_product_user x
    on u.user_id = x.user_id
    where x.product_id = @product_id
    order by u.Initials

    if left(@Initials, 2) = ', '
        set @Initials = substring(@Initials, 3, len(@Initials) - 2)

    return @Initials
END

--AND HERE'S HOW TO CALL IT

select p.name, p.description, dbo.GetInitialsForProduct(p.product_id) as Initials
from tbl_product p

這篇關(guān)于組合子查詢中的行的 Select 語句(樞軸)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Modify Existing decimal places info(修改現(xiàn)有小數(shù)位信息)
The correlation name #39;CONVERT#39; is specified multiple times(多次指定相關(guān)名稱“CONVERT)
T-SQL left join not returning null columns(T-SQL 左連接不返回空列)
remove duplicates from comma or pipeline operator string(從逗號或管道運算符字符串中刪除重復(fù)項)
Change an iterative query to a relational set-based query(將迭代查詢更改為基于關(guān)系集的查詢)
concatenate a zero onto sql server select value shows 4 digits still and not 5(將零連接到 sql server 選擇值仍然顯示 4 位而不是 5)
主站蜘蛛池模板: 一级毛片视频在线观看 | 国产美女黄色 | 九九天堂网 | 日韩视频在线观看一区二区 | 精品国产乱码久久久久久牛牛 | 国产区在线视频 | 精品久久久久久亚洲精品 | 亚洲国产精品一区二区久久 | 91久久精品视频 | 国产一区二区电影 | h视频在线免费 | 日韩中文一区二区三区 | 中文字幕在线一区二区三区 | 精品99在线 | 日本a∨视频 | 日日做夜夜爽毛片麻豆 | 日本成人中文字幕在线观看 | 亚洲精品国产一区 | 一区二区三区在线电影 | 国产精品特级片 | 久久久久久久久综合 | 九色在线| 国产亚洲www| 中文亚洲视频 | 欧美色综合| 久久久免费毛片 | 一区二区三区免费 | www.久久国产精品 | 成人国产精品久久 | 久久伊人在 | 久久一级大片 | 中文字幕一区在线 | 欧美日韩亚洲国产 | 欧美a在线 | 亚洲成人精品在线观看 | 欧美一区二区三区在线观看视频 | 午夜精品一区二区三区免费视频 | 日本视频在线 | 欧美久久久久久久 | 可以免费观看的av片 | 国产中文字幕网 |