产品表  product
类别编号    classId
所属公司    companyId
公司表 company
编号   companyId合作伙伴主表  Friend
编号  MainId  自增
公司编号     companyId
合作公司编号  friendId
合作伙伴明细表 FriendDetail
编号   ID  自增
主表编号  MainId
产品类别编号  classId
产品类别表 class
编号  classid 表设计是以上的,现在我想要输入一个公司编号,将属于其合作伙伴的合作产品类别的产品查找出来。
我试了好一会,没试出来,把自己的逻辑搞乱了!主要是卡在合作伙伴的类别上,那类别是所有产品通用的,也就是每个公司都使用一样的类别

解决方案 »

  1.   

    select a.* , b.* , c.* , d.*
    from company a , Friend b, FriendDetail c , product d
    where a.companyId = b.companyId and b.MainId = c.MainId and c.classId = d.classId
    and a.companyId = '...'
      

  2.   

    product 中的classId对应class表中的classid,
    companyId对应company表中的companyId。
    Friend表中的
    companyId,friendId对应company表中的companyId,
    也就是一个公司会有多个合作伙伴。
    FriendDetail表中的MainId对应Friend表中的MainId,classId对应class表中的classid,也就是每个合作伙伴公司会有多个产品类别。
    我想着这关系一眼就可以看出来~~就没说!
      

  3.   

    最好给出完整的表结构,测试数据,计算方法和正确结果.发帖注意事项
    http://topic.csdn.net/u/20091130/21/fb718680-98ff-4afb-98d8-cff2f8293ed5.html?24281
      

  4.   

    select
      *
    from
      product a,company b,Friend c,FriendDetail d,class e
    where
      a.classId=e.classId 
    and
      c.companyId=b.companyId 
    and
      ...
      

  5.   


      select * from product where classId  in (
      select classId  from FriendDetail where MainId in (
       select MainId  from Friend where companyId = "公司编号"))
      
      where条件中用in取出符合条件的数据,根据表结构关系得到产品信息。
      

  6.   


    CREATE TABLE [ManageFriend] (
    [Manage_id] [bigint] NOT NULL ,
    [Friend_id] [bigint] NULL ,
    [ManageFriend_Id] [bigint] IDENTITY (1, 1) NOT FOR REPLICATION  NOT NULL ,
    CONSTRAINT [PK_ManageFriend] PRIMARY KEY  CLUSTERED 
    (
    [ManageFriend_Id]
    )  ON [PRIMARY] 
    ) ON [PRIMARY]
    GOCREATE TABLE [ManageFriendDeail] (
    [ManageFriendId] [bigint] NULL ,
    [FriendDetailId] [int] NULL ,
    [AX_ID] [bigint] IDENTITY (1, 1) NOT FOR REPLICATION  NOT NULL ,
    CONSTRAINT [PK_ManageFriendDeail] PRIMARY KEY  CLUSTERED 
    (
    [AX_ID]
    )  ON [PRIMARY] 
    ) ON [PRIMARY]
    GO
    CREATE TABLE [YX_Manage] (   --此表对应编号就是companyId
    [YX_ID] [int] IDENTITY (1, 1) NOT FOR REPLICATION  NOT NULL ,
    [AX_Name] [nvarchar] (200) COLLATE Chinese_PRC_CI_AS NULL ,
    ) ON [PRIMARY]
    GOCREATE TABLE [YX_Product] (
    [YX_ID] [int] IDENTITY (1, 1) NOT FOR REPLICATION  NOT NULL ,
    [YX_Class] [int] NULL ,
    [YX_ProductName] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
    [YX_CompanyID] [int] NULL ,
    CONSTRAINT [PK_YX_Product] PRIMARY KEY  CLUSTERED 
    (
    [YX_ID]
    )  ON [PRIMARY] 
    ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
    GOCREATE TABLE [YX_ProductClass] (
    [YX_ID] [int] IDENTITY (1, 1) NOT NULL ,
    [YX_ClassName] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
    ) ON [PRIMARY]
    GO
    /*
    ManageFriend表中Manage_id、Friend_id对应YX_Manage表中YX_ID.
    ManageFriendDeail表中ManageFriendId对应ManageFriend表中主健ManageFriend_Id,FriendDetailId对应YX_ProductClass表中YX_ID.
    YX_Product表中YX_Class对应YX_ProductClass表中YX_ID,YX_CompanyID对应YX_Manage表中YX_ID
    以上表中没说明的字段要么为主健,要么是为了让大家容易理解写上去的,去掉了不相干的字段。*/
    /*
    ManageFriend 数据如下:
    Manage_id,Friend_id,ManageFriend_Id
    11,10,1
    10,11,2
    13,10,4
    13,11,5ManageFriendDeail数据如下:
    ManageFriendId,FriendDetailId,AX_ID
    4,17,1
    4,7,2
    4,15,3
    5,17,4
    5,7,5
    5,15,6
    4,21,7YX_Manage数据如下:
    YX_ID,AX_Name
    8,"测试1"
    9,"测试2"
    7,"测试31"
    10,"测试4"
    11,"测试5"
    13,"测试6"YX_Product数据如下:
    YX_ID,YX_Class,YX_ProductName,YX_CompanyID
    54,17,"text1",11YX_ProductClass数据如下:
    YX_ID,YX_ClassName
    18,"男装"
    19,"外套"
    20,"T恤"
    21,"项链"
    22,"项链"
    6,"女装"
    7,"裤子"
    8,"裙子"
    15,"大衣"
    16,"毛衣"
    17,"外套"
    */
    当产品中YX_CompanyID编号为13时,根据关联应该查找出product表中的那一条数据。
      

  7.   

    SELECT * FROM dbo.YX_Manage A 
    ,dbo.ManageFriend B ,dbo.ManageFriendDeail C,dbo.YX_Product D
    WHERE
    A.YX_ID=D.YX_CompanyID
    and
    A.YX_ID=B.Manage_id 
    and
    B.ManageFriend_Id=C.ManageFriendId
    andA.YX_ID=13
    我用这种办法没查出来,我开始用的是左联接。我上面写的东西可以直接写进数据库里,数据是导出txt文档里的数据,去掉了些不必要的字段。
      

  8.   

    我把逻辑性说一次吧,具体数据和表结构请看十六楼当条件Manage_id为13的时候,它的合作伙伴有两个10,11,对应的唯一编号为4,5,其对应的类别明细为4->7、15、17、21;5->7、15、17;
    现在product 表中有数据一条是符合条件的。
      

  9.   

    为了避免查询出错,我把容易出错的数据也写一条吧。
    YX_Product数据如下:
    YX_ID,YX_Class,YX_ProductName,YX_CompanyID
    54,17,"text1",11
    55,21,"text1",10
      

  10.   

    Select * From   product a Left Join  company b On b.companyID=a.companyID 
                              Left Join  Friend c On b.companyID=c.companyID
                              Left Join  FriendDetail d On d.MainId=c.MainId
                              Left Join  Class  e On e.classid=d.classid  
                              Where b.companyID ='……'
      

  11.   

    select a.* , b.* , c.* , d.* 
    from company a , Friend b, FriendDetail c , product d 
    where a.companyId = b.companyId and b.MainId = c.MainId and c.classId = d.classId 
    and a.companyId = '...' 
      

  12.   

    建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果
       参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
       
       1. 你的 create table xxx .. 语句
       2. 你的 insert into xxx ... 语句
       3. 结果是什么样,(并给以简单的算法描述)
       4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
       
       这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。   
      

  13.   


    公司表 company ,合作伙伴主表  Friend ,合作伙伴明细表 FriendDetail 
    三个表直接关联就可以呀
      

  14.   

    合作伙伴明细表有个问题,就是写的时候不能用in,要不查询数据会出问题。
    我倒是有个办法,就是使用游标来一条条比对放入临时表,然后再从临时表查出来,只是繁锁了些,我想看看有没一句sql能搞定的办法。
      

  15.   


    可能你没仔细想过,如果是直接关联的话,这里有两个条件要用in就是产品类别和公司编号,你看一下我16楼给出的数据和22楼给的数据,product表中数据为二十二楼的数据将会全部查出来,所以就得根据实际的一条一条精确比对查询。直接连就是这个问题了,关键是有两个IN,里面数据会乱掉。。我用的下面办法做的,不可能会出错。。
    /*                  
     *  功能:              
     *  创建人:Jam.Fu              
     *  创建时间:2009/12/21                
     *               
     */                
    alter FUNCTION GetAllProductID(@companyId INT)                
    RETURNS @retFindReports TABLE (proid int)
                   
    as 
    BEGIN
     DECLARE @reports TABLE (proid int)DECLARE @FRIENDID BIGINT,@CLASSID INT
    DECLARE MY_cursor CURSOR FOR         
    SELECT  
    b.Friend_id,
    c.FriendDetailId
    FROM   
    dbo.YX_Manage a 
    LEFT JOIN  dbo.ManageFriend  b
    ON  b.Manage_id= a.YX_ID  
    LEFT JOIN dbo.ManageFriendDeail c
    ON c.ManageFriendId=b.ManageFriend_Id 
    WHERE
    a.AX_IsOpenFriend=0 
    AND 
    a.YX_ID=@companyIdOPEN MY_cursor   FETCH NEXT FROM MY_cursor               
       INTO @FRIENDID, @CLASSID             
       WHILE @@FETCH_STATUS = 0              
       BEGIN  
    INSERT INTO 
    @reports 
    SELECT 
    YX_ID as proid
    FROM 
    dbo.YX_Product 
    WHERE
    YX_Class=@CLASSID
    AND
    YX_CompanyID=@FRIENDID

      FETCH NEXT FROM MY_cursor               
         INTO @FRIENDID, @CLASSID  
       END              
       CLOSE MY_cursor              
       DEALLOCATE MY_cursor  
       INSERT INTO
    @retFindReports
       SELECT 
    proid
       FROM
    @reports
      RETURN
    END
    避免我说的情况出现。。