SQL - 如何在查询中使用 "CROSS APPLY"
SQL - How To Use "CROSS APPLY" In Query
我有两个 table(轮播 和 carousel_images)
轮播 table
id
small_img_1
small_img_2
large_img_1
large_img_2
img_cta_1
img_cta_2
75
101
102
103
104
cta_1
cta_2
76
201
202
203
204
xxx
xxx
carousel_images table
id
url
alt_tag
101
http_101
text_101
102
http_102
text_102
103
http_103
text_103
104
http_104
text_104
预期结果我想return以下结果。选择构建旋转木马所需的所有图像,其中 carousel.id == 1
.
img_id
small_img
small_img_url
small_img_alt
large_img
large_img_url
large_img_alt
img_cta
1
101
http_101
text_101
103
http_103
text_103
cta_1
2
102
http_102
text_102
104
http_104
text_104
cta_2
问题 - 这就是我已经走了多远。我无法在我的结果中获得 large_img_1
和 large_img_2
的结果。
img_id
small_img
small_img_url
small_img_alt
img_cta
1
101
http_101
text_101
cta_1
2
102
http_102
text_102
cta_2
这是我目前的查询。
SELECT
[img_id],
[small_img],
[large_img] [img_cta],
[carousel_images].[url] AS 'small_img_url',
[carousel_images].[alt_tag] AS 'small_img_alt'
FROM
[dbo].[carousels] CROSS APPLY(
VALUES
(1, [small_img_1], [img_cta_1]),
(2, [small_img_2], [img_cta_2])
) V(img_id, small_img, img_cta)
LEFT OUTER JOIN [carousel_images] ON [small_img] = [carousel_images].id
WHERE
[carousel_images] = 75;
有人可以帮忙吗?我已经卡了好几天了。
它也是一个遗留系统,所以这就是为什么 table 结构是这样的。不幸的是我不能改变它:(
你快到了。
您需要将 large_img
添加到 CROSS APPLY
。
并加入 carousel_images
两次。一次用于 small_img
一次用于 large_img
SELECT
v.[img_id],
v.[small_img],
s.[url] AS small_img_url,
s.[alt_tag] AS small_img_alt,
v.[large_img],
l.[url] AS large_img_url,
l.[alt_tag] AS large_img_alt,
v.[img_cta]
FROM
[dbo].[carousels] c
CROSS APPLY
(
VALUES
(1, [small_img_1], [large_img_1], [img_cta_1]),
(2, [small_img_2], [large_img_2], [img_cta_2])
) v (img_id, small_img, large_img, img_cta)
LEFT OUTER JOIN [dbo].[carousel_images] s ON v.[small_img] = s.id
LEFT OUTER JOIN [dbo].[carousel_images] l ON v.[large_img] = l.id
我有两个 table(轮播 和 carousel_images)
轮播 table
id | small_img_1 | small_img_2 | large_img_1 | large_img_2 | img_cta_1 | img_cta_2 |
---|---|---|---|---|---|---|
75 | 101 | 102 | 103 | 104 | cta_1 | cta_2 |
76 | 201 | 202 | 203 | 204 | xxx | xxx |
carousel_images table
id | url | alt_tag |
---|---|---|
101 | http_101 | text_101 |
102 | http_102 | text_102 |
103 | http_103 | text_103 |
104 | http_104 | text_104 |
预期结果我想return以下结果。选择构建旋转木马所需的所有图像,其中 carousel.id == 1
.
img_id | small_img | small_img_url | small_img_alt | large_img | large_img_url | large_img_alt | img_cta |
---|---|---|---|---|---|---|---|
1 | 101 | http_101 | text_101 | 103 | http_103 | text_103 | cta_1 |
2 | 102 | http_102 | text_102 | 104 | http_104 | text_104 | cta_2 |
问题 - 这就是我已经走了多远。我无法在我的结果中获得 large_img_1
和 large_img_2
的结果。
img_id | small_img | small_img_url | small_img_alt | img_cta |
---|---|---|---|---|
1 | 101 | http_101 | text_101 | cta_1 |
2 | 102 | http_102 | text_102 | cta_2 |
这是我目前的查询。
SELECT
[img_id],
[small_img],
[large_img] [img_cta],
[carousel_images].[url] AS 'small_img_url',
[carousel_images].[alt_tag] AS 'small_img_alt'
FROM
[dbo].[carousels] CROSS APPLY(
VALUES
(1, [small_img_1], [img_cta_1]),
(2, [small_img_2], [img_cta_2])
) V(img_id, small_img, img_cta)
LEFT OUTER JOIN [carousel_images] ON [small_img] = [carousel_images].id
WHERE
[carousel_images] = 75;
有人可以帮忙吗?我已经卡了好几天了。
它也是一个遗留系统,所以这就是为什么 table 结构是这样的。不幸的是我不能改变它:(
你快到了。
您需要将 large_img
添加到 CROSS APPLY
。
并加入 carousel_images
两次。一次用于 small_img
一次用于 large_img
SELECT
v.[img_id],
v.[small_img],
s.[url] AS small_img_url,
s.[alt_tag] AS small_img_alt,
v.[large_img],
l.[url] AS large_img_url,
l.[alt_tag] AS large_img_alt,
v.[img_cta]
FROM
[dbo].[carousels] c
CROSS APPLY
(
VALUES
(1, [small_img_1], [large_img_1], [img_cta_1]),
(2, [small_img_2], [large_img_2], [img_cta_2])
) v (img_id, small_img, large_img, img_cta)
LEFT OUTER JOIN [dbo].[carousel_images] s ON v.[small_img] = s.id
LEFT OUTER JOIN [dbo].[carousel_images] l ON v.[large_img] = l.id