Sql 按具有联接、左联接和并集的子查询排序

Sql 按具有联接、左联接和并集的子查询排序,sql,join,subquery,left-join,union,Sql,Join,Subquery,Left Join,Union,我的问题是:如何根据位置AssetId对子查询排序,然后根据表TrxAssetPool对其相关的物理AssetId排序 我需要左路连接,因为不是所有的位置和身体连接在一起。一些位置/物理位置是独立的。物理可能存在于PhysicalAsset和TrxPhysicalAsset中,但不存在于TrxAssetPool中,因为它未链接到任何位置;反之亦然。这些数据也需要显示出来 CREATE TABLE `PositionAssets` ( `Id` int(5) unsigned NOT NULL

我的问题是:如何根据位置AssetId对子查询排序,然后根据表TrxAssetPool对其相关的物理AssetId排序

我需要左路连接,因为不是所有的位置和身体连接在一起。一些位置/物理位置是独立的。物理可能存在于PhysicalAsset和TrxPhysicalAsset中,但不存在于TrxAssetPool中,因为它未链接到任何位置;反之亦然。这些数据也需要显示出来

CREATE TABLE `PositionAssets` (
  `Id` int(5) unsigned NOT NULL,
  `Code` varchar(50) NOT NULL,
  `Desc` varchar(200) NOT NULL,
  PRIMARY KEY (`Id`)
);
CREATE TABLE `PhysicalAssets` (
  `Id` int(5) unsigned NOT NULL,
  `Code` varchar(50) NOT NULL,
  `Desc` varchar(200) NOT NULL,
  PRIMARY KEY (`Id`)
);
CREATE TABLE `TrxPositionAssets` (
  `Id` int(5) unsigned NOT NULL,
  `MaintTrxId` int(5) unsigned NOT NULL,
  `PositionAssetId` int(5) NOT NULL,
  PRIMARY KEY (`Id`,`MaintTrxId`)
);
CREATE TABLE `TrxPhysicalAssets` (
  `Id` int(5) unsigned NOT NULL,
  `MaintTrxId` int(5) unsigned NOT NULL,
  `PhysicalAssetId` int(5) NOT NULL,
  PRIMARY KEY (`Id`,`MaintTrxId`)
);
CREATE TABLE `TrxAssetPool` (
  `Id` int(5) unsigned NOT NULL,
  `MaintTrxId` int(5) NOT NULL,
  `PositionAssetId` int(5) NOT NULL,
  `PhysicalAssetId` int(5) NOT NULL,
  PRIMARY KEY (`Id`)
);
INSERT INTO `PositionAssets` (`Id`, `Code`, `Desc`) VALUES
  ('1', 'PositionC', 'Air conditioner'),
  ('2', 'PositionB', 'Laptop'),
  ('3', 'PositionA', 'Mobile Phone')
  ;
INSERT INTO `PhysicalAssets` (`Id`, `Code`, `Desc`) VALUES
  ('1', 'PhysicalD', 'Dunlop Car Tyre'),
  ('2', 'PhysicalA1', 'Samsung'),
  ('3', 'PhysicalB2', 'Acer'),
  ('4', 'PhysicalB1', 'Lenovo')
  ;
INSERT INTO `TrxPositionAssets` (`Id`, `MaintTrxId`, `PositionAssetId`) VALUES
  ('1', '1', '2'),
  ('2', '1', '3'),
  ('3', '1', '1')
  ;
INSERT INTO `TrxPhysicalAssets` (`Id`, `MaintTrxId`, `PhysicalAssetId`) VALUES
  ('1', '1', '2'),
  ('2', '1', '3'),
  ('3', '1', '1'),
  ('4', '1', '4')
  ;
INSERT INTO `TrxAssetPool` (`Id`,`MaintTrxId`,`PositionAssetId`,`PhysicalAssetId`) VALUES
  ('1', '1', '3', '2'),
  ('2', '1', '2', '4'),
  ('3', '1', '2', '3')
  ; 

SELECT DataType, DataCode, DataDesc 
FROM ( 
    SELECT 'Position' AS DataType, pos.Code AS DataCode, pos.Desc AS DataDesc
        FROM TrxPositionAssets trxpos
        JOIN PositionAssets pos ON pos.Id = trxpos.PositionAssetId
        LEFT JOIN TrxAssetPool trxpool ON (trxpool.PositionAssetId = trxpos.PositionAssetId and trxpool.MaintTrxId = trxpos.MaintTrxId)
        WHERE trxpos.MaintTrxId = 1
    UNION
    SELECT 'Physical' AS DataType, phy.Code AS DataCode, phy.Desc  AS DataDesc
        FROM TrxPhysicalAssets trxphy
        JOIN PhysicalAssets phy ON phy.Id = trxphy.PhysicalAssetId
        LEFT JOIN TrxAssetPool trxpool ON (trxpool.PhysicalAssetId = trxphy.PhysicalAssetId and trxpool.MaintTrxId = trxphy.MaintTrxId)
        WHERE trxphy.MaintTrxId = 1  
) DataPool

当前结果:

DataType    DataCode    DataDesc

Position    PositionA   Mobile Phone
Position    PositionB   Laptop
Position    PositionC   Air conditioner
Physical    PhysicalA1  Samsung
Physical    PhysicalB1  Lenovo
Physical    PhysicalB2  Acer
Physical    PhysicalD   Dunlop Car Tyre
预期结果:

DataType    DataCode    DataDesc

Position    PositionA   Mobile Phone
Physical    PhysicalA1  Samsung
Position    PositionB   Laptop
Physical    PhysicalB1  Lenovo
Physical    PhysicalB2  Acer
Position    PositionC   Air conditioner
Physical    PhysicalD   Dunlop Car Tyre

空调与任何物理因素无关。Dunlop汽车轮胎与任何位置无关。

在查询结束时

ORDER BY DATA.DataId ASC;

在查询put的末尾

ORDER BY DATA.DataId ASC;

要解决你的问题,你必须简化它,并一步一步地解决它,这样会更容易找到解决方案

例如,简单地连接两个表

SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID;

要解决你的问题,你必须简化它,并一步一步地解决它,这样会更容易找到解决方案

例如,简单地连接两个表

SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers ON Orders.CustomerID=Customers.CustomerID;

您需要在子查询中选择所需的信息。另外,
左连接是不必要的,因为它们是由
WHERE
撤消的,您可能需要
全部联合

SELECT Data.[DataId], Data.[TrxnDataId], Data.[Type]
FROM ((SELECT pa.[Id] AS DataId, tpa.[Id] AS TrxnDataId, 'Position' AS Type,
              tap.PositionAssetId, 1 as ord
       FROM {TrxPositionAssets} tpa JOIN
            {PositionAssets} pa
            ON pa.[Id] = tpa.[PositionAssetId] JOIN
            TrxAssetPool} tap
            ON tap.[PositionAssetId] = pa.[Id] AND tap.[TrxId] = tpa.[TrxId])
         WHERE tpa.[TrxId] = @TrxId 
      ) UNION ALL
      (SELECT pa.[Id] AS DataId, tpa.[Id] AS TrxnDataId, 'Physical' AS Type,
              tap.PositionAssetId, 2 as ord
       FROM {TrxPhysicalAssets} tpa JOIN
            {PhysicalAssets} pa
            ON pa.[Id] = tpa.[PhysicalAssetId] JOIN
            {TrxAssetPool} tap
            ON tap.[PhysicalAssetId] = pa.[Id] AND tap.[TrxId] = tpa.[TrxId]
       WHERE tpa.[TrxId] = @TrxId
      )
     ) data 
ORDER BY PositionAssetId, ord, dataId;

您需要在子查询中选择所需的信息。另外,
左连接是不必要的,因为它们是由
WHERE
撤消的,您可能需要
全部联合

SELECT Data.[DataId], Data.[TrxnDataId], Data.[Type]
FROM ((SELECT pa.[Id] AS DataId, tpa.[Id] AS TrxnDataId, 'Position' AS Type,
              tap.PositionAssetId, 1 as ord
       FROM {TrxPositionAssets} tpa JOIN
            {PositionAssets} pa
            ON pa.[Id] = tpa.[PositionAssetId] JOIN
            TrxAssetPool} tap
            ON tap.[PositionAssetId] = pa.[Id] AND tap.[TrxId] = tpa.[TrxId])
         WHERE tpa.[TrxId] = @TrxId 
      ) UNION ALL
      (SELECT pa.[Id] AS DataId, tpa.[Id] AS TrxnDataId, 'Physical' AS Type,
              tap.PositionAssetId, 2 as ord
       FROM {TrxPhysicalAssets} tpa JOIN
            {PhysicalAssets} pa
            ON pa.[Id] = tpa.[PhysicalAssetId] JOIN
            {TrxAssetPool} tap
            ON tap.[PhysicalAssetId] = pa.[Id] AND tap.[TrxId] = tpa.[TrxId]
       WHERE tpa.[TrxId] = @TrxId
      )
     ) data 
ORDER BY PositionAssetId, ord, dataId;

您如何知道哪个位置资产与哪个物理资产匹配?@GordonLinoff它链接在TrxAssetPool中,该TrxAssetPool存在PositionAssetId和PhysicalAssetId字段用自然语言重复我们可以在代码中读取的内容是没有意义的。告诉我们输出表是如何作为输入表的函数的——结果中有一行值(DataId,…),如果&仅当输入中有什么?我们不需要链接/图像,我们需要DDL和其他的--cut&paste&runnable代码,包括初始化代码((表格格式)和所需的结果(最好是有序的,所以可以区分)&clear规范。这可能是一个常见问题解答。请始终使用谷歌搜索错误消息和您的问题/问题/目标的许多清晰、简明和具体的版本/措辞,带有或不带有您的特定字符串/名称和“site:stackoverflow.com”&标记并阅读许多答案。将您发现的相关关键字添加到搜索中。如果您没有找到回答,然后发布,使用1个变量搜索作为标签的标题和关键字。请参阅向下投票箭头鼠标悬停文本。如果您有不重复的代码问题要发布,请阅读并采取行动。您如何知道哪个位置资源与哪个物理资源匹配?@GordonLinoff它链接到存在PositionAssetId和PhysicalAssetId的TrxAssetPool中字段用自然语言重复我们可以在代码中读取的内容是没有意义的。告诉我们输出表是输入表的一个函数——一行值(DataId,…)在结果中的存在当且仅当输入中有什么?我们不需要链接/图像,我们需要DDL和其他的——剪切粘贴&可运行代码,包括初始化代码((表格格式)具有所需结果(最好是有序的,因此可扩散)&clear规范。这可能是一个常见问题解答。请始终使用谷歌搜索错误消息和您的问题/问题/目标的许多清晰、简明和具体的版本/措辞,带有或不带有您的特定字符串/名称和“site:stackoverflow.com”&标记并阅读许多答案。将您发现的相关关键字添加到搜索中。如果您没有找到回答后发布,使用1个变体搜索作为标签的标题和关键字。请参阅向下投票箭头鼠标悬停文本。如果您有不重复的代码问题要发布,请阅读并执行。我想我忘了提到为什么我需要左加入,这是因为并非所有位置和物理链接在一起。一些位置/物理是独立的。PPhysicalAsset和TrxPhysicalAsset中可能存在物理数据,但TrxAssetPool中不存在物理数据,因为它未链接到任何位置,反之亦然。这些数据也需要显示。顺便说一下,感谢您的帮助。(^^^,)我想我忘了提到为什么我需要左连接,这是因为并非所有位置和物理都链接在一起。一些位置/物理是独立的。物理可能存在于PhysicalAsset和TrxPhysicalAsset中,但不存在于TrxAssetPool上,因为它没有链接到任何位置,反之亦然。这些数据也需要由t显示顺便说一句,谢谢你帮助我的努力。(^^,)呃..谢谢..但我想这并不像你的例子那么简单。(^。^)呃..谢谢..但我想这并不像你的例子那么简单。(^。^)谢谢..但我不能这样做,因为Id永远不会按顺序排序。(^。^)谢谢..但我不能这样做,因为Id永远不会按顺序排序。(^。^)