DROP TABLE IF EXISTS `customertemplate`;
CREATE TABLE IF NOT EXISTS "customertemplate" (
  "CID" int(11) NOT NULL,
  "CustomerID" int(11) NOT NULL,
  "TemplateID" int(11) NOT NULL,
  "TemplateType" varchar(10) NOT NULL,
  PRIMARY KEY  ("CID")
) AUTO_INCREMENT=7 ;--
-- Dumping data for table `customertemplate`
--INSERT INTO `customertemplate` (`CID`, `CustomerID`, `TemplateID`, `TemplateType`) VALUES
(1, 8, 1, '1'),
(2, 8, 2, '2'),
(3, 8, 3, '1'),
(4, 8, 4, '2,3'),
(6, 7, 4, '1');-- ----------------------------------------------------------
-- Table structure for table `template`
--DROP TABLE IF EXISTS `template`;
CREATE TABLE IF NOT EXISTS "template" (
  "TemplateID" int(11) NOT NULL,
  "TemplateName" varchar(30) NOT NULL,
  "TemplateFileName" varchar(150) NOT NULL,
  PRIMARY KEY  ("TemplateID")
) AUTO_INCREMENT=6 ;--
-- Dumping data for table `template`
--INSERT INTO `template` (`TemplateID`, `TemplateName`, `TemplateFileName`) VALUES
(1, 'Gloveco', 'Gloveco'),
(2, 'Medline', 'Medline'),
(3, 'MedLine Invoice', 'invoice_medline'),
(4, 'MedLine PackingList', 'PackingList_medline'),
(5, 'Delivery Order', 'Delivery Order');select * from template t left join customertemplate ct on t.TemplateID=ct.TemplateID where ct.CustomerID=8 这样只能查询出来id=8的。
我要显示所有的template,以及在ct表里有的数据,,
TemplateName  TemplateFileName  CID  CustomerID  TemplateID  TemplateType  
  Gloveco         Gloveco        1        8           1          1 
  Medline         Medline        2        8           2          2 
  MedLine Invoice invoice        3        8           3          1 
MedLine PackingList PackingList_medline 4 8           4          2,3 
   xx              xxx           null    null        null        null
   yy              yy大概就是这样,有点表达不清
另外,是mysql,尽量给我mysql能支持的答案。。

解决方案 »

  1.   

    select * from template t left join customertemplate ct on t.TemplateID=ct.TemplateID and ct.CustomerID=8
      

  2.   


    select 
    a.TemplateID,a.TemplateName,a.TemplateFileName,b.CID,b.CustomerID,b.TemplateID,b.TemplateType
    from template a left join customertemplate b on a.TemplateID=b.TemplateID and  b.CustomerID=8
      

  3.   

    FIREGUNS(菜的不能再菜,比初学还初学) ( ) 信誉:100  2007-08-04 09:24:35  得分: 0  
     
     
       结贴
    想明白了。。逻辑错误。。
    换方法写了
      
     
    --------------將你的where改為and即可