久久久久久久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)
主站蜘蛛池模板: 午夜在线视频一区二区三区 | 国产精品a一区二区三区网址 | 亚洲国产成人av好男人在线观看 | 成人网在线观看 | a毛片视频网站 | 狠狠综合久久av一区二区小说 | 精品国产乱码久久久久久丨区2区 | 成人小视频在线免费观看 | 中文在线一区二区 | 国产欧美在线一区 | 国产高清精品一区二区三区 | 中文在线一区二区 | 日韩精品 | av手机在线看| 午夜影院污 | 精品久久香蕉国产线看观看亚洲 | 狠狠av| 欧美性video 精品亚洲一区二区 | 在线观看欧美日韩视频 | 91国内精精品久久久久久婷婷 | 99re6在线视频精品免费 | 精品一区二区三区四区 | 亚洲午夜av久久乱码 | 综合久久一区 | 99视频 | 国产精品久久久久久一区二区三区 | 国产亚洲精品精品国产亚洲综合 | 久久青| 国产精品久久国产精品 | 久久亚| h在线免费观看 | 国产国拍亚洲精品av | 九九久久精品视频 | 一区二区在线看 | 一区二区三区久久 | 欧美日韩黄色一级片 | 国产在线一区二区三区 | 亚洲成人精品在线 | 国产精品一区二区在线播放 | 国产网站在线免费观看 | 男女视频在线观看网站 |