简体   繁体   中英

How to get result of LEFT join into another table as one field

Not sure how to describe this so I will show example:

table PAGES

id      int
parent  int
name    nvarchar
status  tinyint

table PAGES_MODULES

id          int 
id_parent   int
module_type nvarchar
module_id   int
status      int

One page can have more than one linked modules. Example records:

id    parent    name     status
1     -1        Xyz      1
2     -1        Yqw      1

id    id_parent    module_type    module_id     status
1     1            ARTICLE        1             1
2     1            GALLERY        2             1
3     2            CATEGORY       3             1

What I need is to create select which will not return 2 results if I do select left join page_modules.

I would like to have select which returns linked modules as this:

id    parent    name     status    modules
1     -1        Xyz      1         ARTICLE GALLERY
2     -1        Yqw      1         CATEGORY

Is that possible?

Thanks.

UPDATE

I have tried COALESE, CROSS APPLY and SELECT within SELECT methods and came to these conclusions:

http://blog.feronovak.com/2011/10/multiple-values-in-one-column-aka.html

Hope I can publish these here, not meaning to spam or something.

You'd need to create a custom aggregate function that could concatenate the strings together, there is no built-in SQL Server function that does this.

You can create a custom aggregate function (assuming your using the latest version of SQL) using a .Net assembly. Here's the MS reference on how to do this (the example in the article is actually for a CONCATENATE function just like you require): http://msdn.microsoft.com/en-us/library/ms182741.aspx

Use group_concat() to smoosh multiple rows' worth of data into a single field like that. Note that it does have a length limit (1024 chars by default), so if you're going to have a zillion records being group_concatted, you'll only get the first few lines worth unless you raise the limit.

SELECT ..., GROUP_CONCAT(modules SEPARATOR ' ')
FROM ...
GROUP BY ...

Note that it IS an aggregate function, so you must have a group-by clause.

-- ==================
-- sample data
-- ==================
declare @pages table
(
    id int,
    parent int,
    name nvarchar(max),
    status tinyint
)

declare @pages_modules table
(
    id int,
    id_parent int,
    module_type nvarchar(max),
    module_id int,
    status int
)

insert into @pages values (1, -1, 'Xyz', 1)
insert into @pages values (2, -1, 'Yqw', 1)

insert into @pages_modules values (1, 1, 'ARTICLE', 1, 1)
insert into @pages_modules values (2, 1, 'GALLERY', 2, 1)
insert into @pages_modules values (3, 2, 'CATEGORY', 3, 1)


-- ==================
-- solution
-- ==================
select 
    *,
    modules = (
        select module_type + ' ' from @pages_modules pm
        where pm.id_parent = p.id
        for xml path('')
    )
from @pages p

You need to join both tables and then GROUP BY by pages.id , pages.parent , pages.status , pages.name and pages.status . Your modules field in your resultset is then a string aggregate function, ie in Oracle LISTAGG(pages_modules.modules, ' ') as modules .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM