从数据库表中的特定列添加数据

时间:2012-05-03 01:19:36

标签: php mysql

我从表中获得这些数据。例如,我有一个名为tbl_points的点表。 tbl_points包含:

ptsID, team1, team2.
1       10      20
2       20      10
3       30      5

如何创建一个总结team1和team2积分的查询?

2 个答案:

答案 0 :(得分:1)

未测试:

   SELECT SUM(p1.team1) AS team1_score
        , SUM(p2.team2) AS team2_score
     FROM tbl_points AS p1
        , tbl_points AS p2

答案 1 :(得分:1)

以下查询:

SELECT 
  SUM(team1) AS team1_points, 
  SUM(team2) AS team2_points
FROM
  tbl_points

将为您提供一行表:

| team1_points | team2_points |
| 60           | 35           |

此外,使用名称tbl_points并不好。最好不要使用前缀。只需为您的表格points命名,就足够明确了。