English 中文(简体)
如何使用SQL和PHP在某个排序组中获取#
原标题:How to get the # in some sorted group using SQL and PHP
  • 时间:2011-05-31 06:34:30
  •  标签:
  • php
  • sql

场景:

我有一个名为gamesstats的游戏统计表。

我得到了一支球队(对我来说,重要的是身份证)、联赛和赛季。

现在,我可以使用我的比赛统计数据来检查球队打了什么比赛,只询问联赛和赛季的情况,主队id或客队id就是我得到的球队id。

从中,我可以获得该队参加的比赛,并通过HomeStatVisitorStat分别询问该主队或客队的本场比赛统计数据。然后我对他们进行平均,并了解这支球队的情况。

现在的问题是:

我该如何构建这三个数组:一个用于球队平均数的数组(简单,完成),一个用于联盟平均数的阵列(简单,只是不要限制球队id),以及一个用于在联盟中球队处于该特定参数的#的数组(比如在投2分时,它在联盟中排名第四(按所有球队的平均数))。

一点sql,让您了解它的外观:

SELECT "All Games" as Flag, count(*) as Games, avg(s.P2M) as P2M, avg(s.P2A) as P2A,  100*avg(s.P2M)/avg(s.P2A) as P2P,
        avg(s.P3M) as P3M, avg(s.P3A) as P3A,  100*avg(s.P3M)/avg(s.P3A) as P3P,
        avg(s.FTM) as FTM, avg(s.FTA) as FTA,  100*avg(s.FTM)/avg(s.FTA) as FTP,
        avg(s.OFFENSE) as OFFENSE, avg(s.DEF) as DEF, avg(s.TOT) as TOT, 
        avg(s.AST) as AST, avg(s.TO) as TurnO, avg(s.ST) as ST, avg(s.FV) as FV, avg(s.CM) as CM, avg(s.PTS) as PTS,
        (avg(s.TOT) + avg(s.AST) + avg(s.ST) + avg(s.PTS) + avg(s.P2M) + avg(s.P3M) + avg(s.FTM) + avg(s.RV) + avg(s.FV))
        - (avg(s.TO) + avg(s.P3A) + avg(s.P3A) + avg(s.FTA) + avg(s.AG) + avg(s.CM)) as EFF
        FROM gamesstats AS gs, stats AS s
        WHERE gs.$homeOrVisitor = s.ID
        AND gs.HomeTeamID = $ESCteam
        AND gs.SeasonStart = $ESCseason
        AND gs.LeagueID = $ESCleague

I would prefer a fast solution because we are talking about huge tables (30k+ stats). Will edit if needed.

最佳回答

对不起,我真的不明白你真正想要的是什么,因为你说的一切都是“轻而易举”的。但我可以建议您加快SQL的速度。您不应该在SQL中使用太多AVG()调用。只需选择您需要的所有不同的AVG(),然后在PHP中计算所有其他值。例如,您应该在SQL中选择avg(s.P3M)作为P3M,avg(s.P3A)作为P3A,但计算P3P as$row[P3P]=100*$row[P3M]/$row[P3A];在php中,以及所有的AVG()总和。PHP比MySql更容易扩展,而且工作速度也更快。

UPD:如果你问如何在某个字段的选择中获得行的位置,只需在该字段值较大的所有行中选择COUNT(),然后加上“1”。

问题回答

暂无回答




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签