这就是我所拥有的:
查询
select
bc.short_desc as cdesc
from
blog_post as bp
left join blog_post_category_link as blpc on bp.post_id = blpc.post_id
inner join blog_category as bc on blpc.cat_id = bc.cat_id
where bp.post_id = 1
结果
----------------
| |cdesc |
----------------
|1|Top level |
----------------
|2|Sub level |
----------------
|3|SubSub level|
----------------
第二次查询
select
bt.short_desc as tdesc
from
blog_post as bp
left join blog_post_tag_link as blpt on bp.post_id = blpt.post_id
inner join blog_tag as bt on blpt.tag_id = bt.tag_id
where bp.post_id = 1
第二个结果
----------------
| |tdesc |
----------------
|1|Tag1 |
----------------
|2|Tag2 |
----------------
加入这两个查询后的所需结果
----------------------
| |cdesc |tdesc|
----------------------
|1|Top level |Tag1 |
----------------------
|2|Sub level |Tag2 |
----------------------
|3|SubSub level| |
----------------------
有可能吗?
答案 0 :(得分:2)
尝试此查询
SET @rn1 = 0;
SET @rn2 = 0;
SELECT t1.rId, t1.cdesc, t2.tdesc
FROM
(select @rn1 := rn1 + 1 AS rId, bc.short_desc as cdesc
from blog_post as bp
left join blog_post_category_link as blpc on bp.post_id = blpc.post_id
inner join blog_category as bc on blpc.cat_id = bc.cat_id
where bp.post_id = 1) t1,
(select @rn2 := rn2 + 1 AS rId, bt.short_desc as tdesc
from blog_post as bp
left join blog_post_tag_link as blpt on bp.post_id = blpt.post_id
inner join blog_tag as bt on blpt.tag_id = bt.tag_id
where bp.post_id = 1) t2
WHERE t1.rId = t2.rId;
对于MS SQL服务器
SELECT t1.rId, t1.cdesc, t2.tdesc
FROM
(select row_number() over(order by bp.post_id) AS rId, bc.short_desc as cdesc
from blog_post as bp
left join blog_post_category_link as blpc on bp.post_id = blpc.post_id
inner join blog_category as bc on blpc.cat_id = bc.cat_id
where bp.post_id = 1) t1,
(select row_number() over(order by bp.post_id) AS rId, bt.short_desc as tdesc
from blog_post as bp
left join blog_post_tag_link as blpt on bp.post_id = blpt.post_id
inner join blog_tag as bt on blpt.tag_id = bt.tag_id
where bp.post_id = 1) t2
WHERE t1.rId = t2.rId;
答案 1 :(得分:0)
使用此模板,只需将上面的查询作为T1和T2的子查询插入:
Select *
from (
) T1
join (
) T2 on T2.tdesc = T1.cdesc and T2.Tag1 = T1.TopLevel and T2.Tag2 = T1.SubLevel
答案 2 :(得分:0)
SELECT
bc.short_desc AS cdesc, bt.short_desc AS tdesc
FROM
blog_post AS bp
LEFT JOIN blog_post_category_link AS blpc ON bp.post_id = blpc.post_id
INNER JOIN blog_category AS bc ON blpc.cat_id = bc.cat_id
LEFT JOIN blog_post_tag_link AS blpt ON bp.post_id = blpt.post_id
INNER JOIN blog_tag AS bt ON blpt.tag_id = bt.tag_id
WHERE bp.post_id = 1