我有一个名为RedMarkSubcategory
的表,例如:
+----------------------+--------+-----------+
| RedMarkSubcategoryId | Name | IsDeleted |
+----------------------+--------+-----------+
| 1 | Item 1 | 0 |
| 2 | Item 2 | 0 |
| 3 | Item 3 | 0 |
+----------------------+--------+-----------+
我还有另一个名为RedMarkItems
的表,其列名为RedMarkSubcategoryId
-像这样:
+----------------+-------+-------+-------+
| RedMArkItemsId | Item1 | Item2 | Item3 |
+----------------+-------+-------+-------+
| 1 | 0 | 0 | 0 |
| 2 | 0 | 0 | 0 |
| 3 | 0 | 0 | 0 |
+----------------+-------+-------+-------+
您可以看到创建的列没有空间。我的问题是,有没有办法将这两个表关联起来以获得类似这样的结果?
SELECT
[RS].[Name], RI.*
FROM
RedMarkSubcategory AS RS
INNER JOIN
RedMarkItems AS [RI] ON RS.Name = "RI.columnName"
WHERE
RS.IsDeleted = 0
答案 0 :(得分:0)
是的,您可以join
表,然后提取值。例如:
SELECT [RS].[Name],
(CASE WHEN RS.NAME = 'Item 1' THEN RI.item1
WHEN RS.NAME = 'Item 2' THEN RI.item2
WHEN RS.NAME = 'Item 3' THEN RI.item3
END) as ri_value
FROM RedMarkSubcategory RS INNER JOIN
RedMarkItems RI
ON RS.Name IN ('Item 1', 'Item 2', 'Item 3')
WHERE RS.IsDeleted = 0