2012-10-31 75 views
0

我有幾個查詢我想合併成1個查詢,我不知道什麼是正確的連接。合併3個SQL查詢,什麼是正確的連接?

此查詢獲取所有匹配的PID的弧線:

var arcs = db.Query("SELECT arc FROM comics WHERE `publisher`=" + pid + " GROUP BY arc").ToList(); 

該查詢會從圓弧表弧的標題:

var ar = db.QuerySingle("SELECT title FROM arcs WHERE id=" + arc.arc); 

此查詢用來獲得總

var issues = db.Query("SELECT id FROM comics WHERE arc=" + arc.arc + " AND publisher=" + pid); 

整個代碼:

0匹配arc.arc和pid的問題的
@{ 
    var pid = Request["pid"]; 

    var db = Database.Open("quickly"); 
    var publisher = db.QuerySingle("SELECT * FROM publishers WHERE [email protected]", pid); 

    Page.Title = publisher.name + " @"; 

    var arcs = db.Query("SELECT arc FROM comics WHERE `publisher`=" + pid + " GROUP BY arc").ToList(); 
} 

<div class="post"> 
    <div class="post-bgtop"> 
     <div class="post-bgbtm"> 
      <h2 class="title">@publisher.name</h2> 
      <div class="entry"> 
       <p> 
        <table width="100%" border="0"> 
         <thead> 
          <tr> 
           <th align="left">Title</th> 
           <th>Issues</th> 
          </tr> 
         </thead> 
         <tbody> 
          @foreach (var arc in arcs) { 
           var ar = db.QuerySingle("SELECT title FROM arcs WHERE id=" + arc.arc); 
           var issues = db.Query("SELECT id FROM comics WHERE arc=" + arc.arc + " AND publisher=" + pid); 
          <tr> 
           <td><a href="@Href("~/Comics/Arcs?aid=" + arc.arc)">@ar.title</a></td> 
           <td align="center">@issues.Count</td> 
          </tr> 
          } 
         </tbody>  
        </table> 
       </p> 
      </div> 
     </div> 
    </div> 
</div> 

表結構:

CREATE TABLE IF NOT EXISTS `arcs` (
    `id` int(255) NOT NULL AUTO_INCREMENT, 
    `title` varchar(255) NOT NULL, 
    `plot` longtext NOT NULL, 
    `added` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, 
    PRIMARY KEY (`id`), 
    UNIQUE KEY `title` (`title`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1; 

CREATE TABLE IF NOT EXISTS `comics` (
    `id` varchar(255) NOT NULL, 
    `arc` int(255) NOT NULL, 
    `title` varchar(255) NOT NULL, 
    `issue` decimal(5,1) DEFAULT NULL, 
    `price` decimal(10,2) NOT NULL, 
    `plot` longtext NOT NULL, 
    `publisher` int(255) NOT NULL, 
    `isbn` varchar(255) NOT NULL, 
    `published` date NOT NULL, 
    `cover` varchar(255) NOT NULL DEFAULT './images/nopic.jpg', 
    `added` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, 
    `views` int(255) NOT NULL DEFAULT '0', 
    `active` int(1) NOT NULL DEFAULT '0', 
    `owned` int(1) NOT NULL DEFAULT '0', 
    PRIMARY KEY (`id`), 
    UNIQUE KEY `arc` (`arc`,`title`,`issue`,`publisher`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1; 

CREATE TABLE IF NOT EXISTS `publishers` (
    `id` int(255) NOT NULL AUTO_INCREMENT, 
    `name` varchar(255) NOT NULL, 
    `plot` longtext NOT NULL, 
    PRIMARY KEY (`id`), 
    UNIQUE KEY `name` (`name`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1; 

回答

1

你必須給我們介紹一下表結構的更多信息。

我的第一種方法是這樣..

Select 
cs.arc as 'Arc', 
arcs.title as 'Arc_Title', 
id as 'Issues' 
from 
Comics cs 
left join Arcs arcs on cs.arc = arcs.id 
left join Comics cs2 on cs.arc = cs2.arc and publisher = @pid 

,但我真的不知道,因爲我猜對錶結構法

編輯...

我明白一個弧可以在多個漫畫中。而且你需要不同的弧線和重複的次數。 因此,我認爲這會工作:

Select 
t1.arc as 'Arc_ID', 
t1.title as 'Arc_Title', 
count(t1.arc) as 'issues' 
from (
Select c.id, c.arc, a.title 
    from comics c 
    left join arcs a on c.arc = a.id 
    where c.publisher = 1) as t1 
group by t1.arc, t1.title 

下面是一個例子.. http://sqlfiddle.com/#!2/a9fda/8/0

+0

哎呀對不起回合的是,我會立即添加。 – rackemup420