2014-12-01 58 views
1

我試圖將表格隊伍的所有城市值更新爲:「city」+「#p」+「玩家人數」+「g」+「前進的目標數量」(例如「Tokyo#p25 g74」)。如何在sql中合併計數值

我試圖做到這一點,並有這兩個查詢。一個用於獲得玩家數量,另一個用於獲得目標數量。

查詢爲玩家的數量:

select t.city + '#p' + CONVERT(varchar(10), count(pt.playerId)) + '#g' 
from team  t, 
     player_team pt 
where pt.teamID = t.teamID 
group By t.teamId,t.city 

查詢的進球數:

select count(*) totalgoals, 
     pt.teamID 
from goals  g, 
     player_team pt 
where g.playerId = pt.playerId 
group by pt.teamID 

我無法合併theese兩項罪名。 幫我出請...

而且我的表層次結構和字段,如下面所示

player 
(
    playerID  int, 
    firstName  nvarchar(25), 
    lastName  nvarchar(25), 
    nationality  varchar(25), 
    birthDate  smalldatetime, 
    age    smallint, 
    position  varchar(25) 
) 

team 
(
    teamID int, 
    name nvarchar(50), 
    city nvarchar(25) 
) 

player_team 
(
    playerID int, 
    teamID  int, 
    season  varchar(5) 
) 

match 
(
    matchID   int, 
    homeTeamID  int, 
    visitingTeamID int, 
    dateOfMatch  smalldatetime, 
    week   tinyint 
) 

goals 
(
    matchID  int, 
    playerID int, 
    isOwnGoal bit, 
    minute  tinyint 
) 

編輯:選擇查詢與下面給出的運作良好,給我正確的results.But怎麼能我用這多個記錄更新表?當我嘗試更新它作爲子查詢到更新語句,它給了我編譯錯誤和抱怨多記錄...

錯誤:子查詢返回多個值。當子查詢遵循=,!=,<,< =,>,> =或當子查詢用作表達式時,這是不允許的。

+2

Mysql,sql-server和splite?那就是很多標籤.. – mxix 2014-12-01 18:47:40

+0

所以在你更新城市之後,當你打進更多的進球時你打算做什麼? – Paparazzi 2014-12-01 19:08:04

+0

SQL Server使用'+'連接,所以我認爲這就是OP正在使用的... – sgeddes 2014-12-01 19:10:50

回答

2

假設你的2個查詢產生預期的效果,嘗試一下用的outer join和子查詢相結合:

select pt.teamId, 
    t.city + ' #p' + CONVERT(varchar(10), count(pt.playerId)) 
     + ' g' + CONVERT(varchar(10), t2.totalgoals) 
from team  t 
    inner join player_team pt on pt.teamID = t.teamID 
    left join (
     select count(*) totalgoals, 
       pt.teamID 
     from goals  g inner join player_team pt on g.playerId = pt.playerId 
     group by pt.teamID 
    ) t2 on t.teamid = t2.teamid 
group By pt.teamId,t.city,t2.totalgoals 
+0

嗯謝謝你。它對我很好.select語句讓我獲得了多個值。所以我該如何更新這個表多個記錄?當我嘗試將它更新爲子查詢時,它會給我編譯錯誤並抱怨多記錄 – Webster 2014-12-01 19:28:39

+1

@Webster - 您可以使用帶有連接的更新:http://sqlfiddle.com/#!3/bdb5b/1。然而,你確定你想更新城市領域,或者你只是想要一個新的視圖與這些數據... – sgeddes 2014-12-01 19:50:28

+0

好吧,非常感謝你,它幫了我很多 – Webster 2014-12-01 20:10:21

-1

試試這個:

SELECT a.city '#p' + CONVERT(varchar(10), a.playerCount) + '#g' + CONVERT(varchar(10), b.totalgoals) 
FROM (SELECT t.teamId, t.city, count(pt.playerId) as playerCount 
     FROM team t JOIN player_team pt on (t.teamID = pt.teamID) 
     GROUP BY t.teamId, t.city)a JOIN 
     (SELECT pt.teamId, count(*) as totalgoals, 
     FROM goals g JOIN player_team pt on (g.playerId = pt.playerId) 
     GROUP BY pt.teamId) b ON (a.teamId = b.teamId) 
1

最簡單的方法可能是使用關聯d子查詢來查找每個團隊的值:

SELECT t.city || ' #p' || 
     CONVERT(varchar(10), (SELECT count(*) 
          FROM player_team 
          WHERE teamId = t.teamId)) 
     || ' #g ' || 
     CONVERT(varchar(10), (SELECT count(*) 
          FROM goals 
          JOIN player_team USING (playerId) 
          WHERE teamId = t.teamId)) 
FROM team t