SQL查询帮助 - 基于条件连接多个列

时间:2011-07-18 15:28:22

标签: mysql sql sql-server sql-server-2008 linq-to-sql

  

可能重复:
  SQL query help - have two where conditons in join condition

我有以下表格,如下所示。我已经提到了我需要的东西。我已经在这里SQL query help - have two where conditons in join condition发布了一个链接,我已经尝试了但是无法通过。再次明确地提出我需要的东西:

Book
BookId, BookName

Desk
DeskId, BookId ,DeskName

CounterParty
CPId, CpName

Trade
TradeId, Buyer, Seller

这是买方和卖方数据的方式:

Buyer Seller
B3232  B323
C32    B222
B323   C323

根据这两列中的起始字符B或C,我需要连接Book或CP表来检查ID。

I need **t.TradingDeskName, b.BookName, c.CpName, t.Buyer, t.Seller.**

非常感谢任何帮助。

谢谢, 马尼

p.s:我试图通过SQL或Linq到Sql来完成这项工作。


最近的查询,但有更多要修复:

SELECT DISTINCT desk.Name as TradingDeskName, b.Name as Book, t.Seller, t.Buyer, c.PartyName, FROM TradingDesk AS desk 

 RIGHT JOIN Book as b
    ON  b.TradingDeskId = d.Id
 RIGHT JOIN Trade as t  
    ON LEFT(t.Buyer, 1) = 'B' AND SUBSTRING(t.Buyer, 2, len(t.Buyer)) = b.Id
 LEFT JOIN Book as b1 
    ON LEFT(t.Seller, 1) = 'B' AND SUBSTRING(t.Seller, 2, len(t.Seller)) = b1.Id 
 LEFT JOIN CounterParty as c 
    ON LEFT(t.Buyer, 1) = 'C' AND SUBSTRING(t.Buyer, 2, len(t.Buyer)) = c.PartyId 
 LEFT JOIN CounterParty as c1 
    ON LEFT(t.Seller, 1) = 'C' AND SUBSTRING(t.Seller, 2, len(t.Seller)) = c1.PartyId

正如我所提到的,我需要: Desk.Name - B.Name - T.Seller - T.Buyer- C.PartyName

如果T.Seller或T.Buyer值以'C'开头(来自CounterParty表),则C.PartyName将具有该值,否则将为null。

通过上面的查询,我在Desk.Name,B.Name和gettig C.PartyName的逻辑中都有空值。


2 个答案:

答案 0 :(得分:0)

你可以这样做(未经测试):

select 
  t.Buyer, 
  t.Seller, 
  case when t.Buyer like 'B%' THEN (select BookName from Book where BookId = t.Buyer)
                              ELSE (select CpName from Counterparty where CPId = t.Buyer)
  end BuyerName,
  case when t.Buyer like 'B%' THEN (select DeskName from Desk where BookId = t.Buyer)
                              ELSE NULL
  end BuyerDeskName,
  case when t.Seller like 'B%' THEN (select BookName from Book where BookId = t.Seller)
                               ELSE (select CpName from Counterparty where CPId = t.Seller)
  end SellerName,
  case when t.Seller like 'B%' THEN (select DeskName from Desk where BookId = t.Seller)
                               ELSE NULL
  end SellerDeskName,
from 
  Trade t

您遇到的问题是,由于您要加入的表是数据驱动的,因此您无法在FROM子句中指定它。

答案 1 :(得分:0)

我可以通过几种方式来实现预期的结果但是因为首先应该首先考虑,所以如果可能的话,我建议修改数据库设计。

所以,以下是我可以解决的2个查询:

查询1

SELECT `t`.*,
(CASE 
    WHEN LEFT(`t`.`Buyer`, 1) = 'B' THEN
        (SELECT `b`.`BookName`
         FROM `Book` `b`
         WHERE `b`.`BookId` = SUBSTRING(`t`.`Buyer`, 2))
    ELSE (SELECT `c`.`CPName`
          FROM `CounterParty` `c`
          WHERE `c`.`CPId` = SUBSTRING(`t`.`Buyer`, 2))
END) AS `buyer_name`,
(CASE
    WHEN LEFT(`t`.`Seller`, 1) = 'B' THEN
        (SELECT `b`.`BookName`
         FROM `Book` `b`
         WHERE `b`.`BookId` = SUBSTRING(`t`.`Seller`, 2))
    ELSE (SELECT `c`.`CPName`
          FROM `CounterParty` `c`
          WHERE `c`.`CPId` = SUBSTRING(`t`.`Seller`, 2))
END) AS `seller_name`
FROM `Trade` `t`

查询2

SELECT *
FROM `Trade` `t`
LEFT JOIN `Book` `b` ON LEFT(`t`.`Buyer`, 1) = 'B' AND SUBSTRING(`t`.`Buyer`, 2) = `b`.`BookId`
LEFT JOIN `Book` `b1` ON LEFT(`t`.`Seller`, 1) = 'B' AND SUBSTRING(`t`.`Seller`, 2) = `b1`.`BookId`
LEFT JOIN `CounterParty` `c` ON LEFT(`t`.`Buyer`, 1) = 'C' AND SUBSTRING(`t`.`Buyer`, 2) = `c`.`CPId`
LEFT JOIN `CounterParty` `c1` ON LEFT(`t`.`Seller`, 1) = 'C' AND SUBSTRING(`t`.`Seller`, 2) = `c1`.`CPId`;

上述两个查询都返回相同的结果,但格式不同。请试着看看哪一个最适合你。

此外,从您的问题中不清楚表格Desk在哪里适合以及它与其他表格有什么关系。请随意从Desk添加您需要的相应列。

请注意,建议的查询是在MySQL中。目前还不是很清楚你在运行什么系统 - 你在帖子中提到过你正在尝试使用SQL或Linq SQL,而你在标签中提到的一切都是+ MySQL。