[英]query from two tables CAST
I have a query from two tables ... 我有两个表的查询...
The first table in which the goods are in the form of tree accounts .... 货物以树状帐单的形式出现的第一个表格....
The second table in which the sale is linked to the first table. 在其中销售与第二个表链接的第二个表。
For example, now I have the first table. 例如,现在我有了第一个表。
Question A sort query is required .. 问题需要排序查询。
I get the cars for their condition ... electronics for their condition 我得到了适合他们状况的汽车...得到了适合他们状况的电子
Of Table II. 表二。
With attachments, I want to create a query. 带有附件,我想创建一个查询。
Database Script : 数据库脚本:
USE [To_Test]
GO
/****** Object: Table [dbo].[Items] Script Date: 11/10/1438 05:54:35 م ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Items](
[Id_ItemS] [int] NOT NULL,
[Name_ar] [nvarchar](50) NULL,
[Number_Id] [int] NULL,
[Basic] [bit] NULL,
CONSTRAINT [PK_Items] PRIMARY KEY CLUSTERED
(
[Id_ItemS] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Sell_items](
[Id_Sell] [int] IDENTITY(1,1) NOT NULL,
[Id_items] [int] NULL,
[price] [money] NULL,
CONSTRAINT [PK_Sell_items] PRIMARY KEY CLUSTERED
(
[Id_Sell] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
INSERT [dbo].[Items] ([Id_ItemS], [Name_ar], [Number_Id], [Basic]) VALUES (1, N'Cars', NULL, 1)
INSERT [dbo].[Items] ([Id_ItemS], [Name_ar], [Number_Id], [Basic]) VALUES (2, N'electronics', NULL, 1)
INSERT [dbo].[Items] ([Id_ItemS], [Name_ar], [Number_Id], [Basic]) VALUES (3, N'Toyota', 1, 1)
INSERT [dbo].[Items] ([Id_ItemS], [Name_ar], [Number_Id], [Basic]) VALUES (4, N'Nissan', 1, 1)
INSERT [dbo].[Items] ([Id_ItemS], [Name_ar], [Number_Id], [Basic]) VALUES (5, N'Camry', 3, 1)
INSERT [dbo].[Items] ([Id_ItemS], [Name_ar], [Number_Id], [Basic]) VALUES (6, N'2015', 5, 0)
INSERT [dbo].[Items] ([Id_ItemS], [Name_ar], [Number_Id], [Basic]) VALUES (7, N'2016', 5, 0)
INSERT [dbo].[Items] ([Id_ItemS], [Name_ar], [Number_Id], [Basic]) VALUES (8, N'2017', 5, 0)
INSERT [dbo].[Items] ([Id_ItemS], [Name_ar], [Number_Id], [Basic]) VALUES (9, N'TV', 2, 1)
INSERT [dbo].[Items] ([Id_ItemS], [Name_ar], [Number_Id], [Basic]) VALUES (10, N'LG', 9, 0)
INSERT [dbo].[Items] ([Id_ItemS], [Name_ar], [Number_Id], [Basic]) VALUES (11, N'Samsung', 9, 0)
SET IDENTITY_INSERT [dbo].[Sell_items] ON
INSERT [dbo].[Sell_items] ([Id_Sell], [Id_items], [price]) VALUES (1, 11, 2500.0000)
INSERT [dbo].[Sell_items] ([Id_Sell], [Id_items], [price]) VALUES (2, 10, 2300.0000)
INSERT [dbo].[Sell_items] ([Id_Sell], [Id_items], [price]) VALUES (3, 6, 20000.0000)
INSERT [dbo].[Sell_items] ([Id_Sell], [Id_items], [price]) VALUES (4, 7, 270000.0000)
SET IDENTITY_INSERT [dbo].[Sell_items] OFF
ALTER TABLE [dbo].[Items] WITH CHECK ADD CONSTRAINT [FK_Items_Items1] FOREIGN KEY([Number_Id])
REFERENCES [dbo].[Items] ([Id_ItemS])
GO
ALTER TABLE [dbo].[Items] CHECK CONSTRAINT [FK_Items_Items1]
GO
ALTER TABLE [dbo].[Sell_items] WITH CHECK ADD CONSTRAINT [FK_Sell_items_Items] FOREIGN KEY([Id_items])
REFERENCES [dbo].[Items] ([Id_ItemS])
GO
ALTER TABLE [dbo].[Sell_items] CHECK CONSTRAINT [FK_Sell_items_Items]
GO
view 视图
WITH Recursive_CTE AS (
SELECT Items.Id_ItemS, Items.Name_ar, Items.Number_Id, 1 AS RecursionLevel, CAST(Items.Id_ItemS AS varchar(100)) AS Hierarchy, Sell_items.Id_Sell
FROM Items INNER JOIN
Sell_items ON Items.Id_ItemS = Sell_items.Id_items
WHERE (Items.Id_ItemS = 1)
UNION ALL
SELECT Items.Id_ItemS, Items.Name_ar, Items.Number_Id, 1 AS RecursionLevel, CAST(Hierarchy + ':' + CAST(Items.Id_ItemS AS varchar(100)) AS varchar(100)) AS Hierarchy, Sell_items.Id_Sell
FROM Recursive_CTE AS parent INNER JOIN
Items ON parent.Id_items = Items.Number_Id INNER JOIN
Sell_items ON Items.Id_ItemS = Sell_items.Id_items )
SELECT * FROM Recursive_CTE ORDER BY Hierarchy
but The result is null 但结果为空
I wish to modify the query ? 我希望修改查询?
The desired result enter image description here 所需结果在此处输入图像描述
I see it as data issue. 我将其视为数据问题。 You don't have any records which satisfy your query.
您没有任何满足您查询条件的记录。
I don't see any items.Id_ItemS = 1
after Inner join on two tables - Items, Sell_items
在两个表-
Items, Sell_items
进行内部items.Id_ItemS = 1
之后,我没有看到任何items.Id_ItemS = 1
SELECT
Items.Id_ItemS,
Items.Name_ar,
Items.Number_Id,
1 AS RecursionLevel,
CAST(Items.Id_ItemS AS varchar(100)) AS Hierarchy,
Sell_items.Id_Sell
FROM Items INNER JOIN Sell_items ON Items.Id_ItemS = Sell_items.Id_items
WHERE (Items.Id_ItemS = 1)
If you remove the where conditions (Items.Id_ItemS = 1) you will get results. 如果删除where条件(Items.Id_ItemS = 1),则会得到结果。
Hope this helps you! 希望这对您有所帮助!
Add the following 3 inserts to Sell_items and you'll get results... 将以下3个插入项添加到Sell_items中,您将获得结果...
INSERT dbo.Sell_items (Id_Sell, Id_items, price) VALUES (5, 1, 270000.0000);
INSERT dbo.Sell_items (Id_Sell, Id_items, price) VALUES (6, 3, 280000.0000);
INSERT dbo.Sell_items (Id_Sell, Id_items, price) VALUES (7, 4, 290000.0000);
Also note that you aren't incriminating your RecursionLevel column. 另请注意,您并没有将RecursionLevel列设为incri。 You'll want to add 1 to the previous value...
您需要将1加到上一个值...
parent.RecursionLevel + 1 AS RecursionLevel,
I came to answer the question, very excellent 我来回答这个问题,非常好
WITH Recursive_CTE (Id_ItemS,Name_ar,Number_Id,RecursionLevel,Hierarchy) AS (
SELECT Items.Id_ItemS, Items.Name_ar, Items.Number_Id, 1 AS RecursionLevel, CAST(Items.Id_ItemS AS varchar(100)) AS Hierarchy
FROM Items
where (Id_ItemS=1)
UNION ALL
SELECT Items.Id_ItemS, Items.Name_ar, Items.Number_Id, RecursionLevel+1 AS RecursionLevel, CAST(Hierarchy + ':' + CAST(Items.Id_ItemS AS varchar(100)) AS varchar(100)) AS Hierarchyss
FROM Recursive_CTE AS parent INNER JOIN
Items ON parent.Id_items = Items.Number_Id)
SELECT Id_ItemS, Name_ar, Number_Id, RecursionLevel, Hierarchy, Sell_items.Id_Sell, Sell_items.price, Sell_items.Id_items2
FROM Recursive_CTE INNER JOIN
Sell_items ON Sell_items.Id_items2 = Recursive_CTE.Id_items
ORDER BY Hierarchy
Id_ItemS Name_ar Number_Id RecursionLevel Hierarchy Id_Sell price Id_items2
6 2015 5 4 1:3:5:6 3 20000.00 6
7 2016 5 4 1:3:5:7 4 270000.00 7
7 2016 5 4 1:3:5:7 5 998877.00 7
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.