我想做的是在表中的ID上获取出现次数,然后根据出现次数将另一列的值除以该值。这是为了更好地说明我正在尝试执行的查询。
SELECT t.t_id, count(DISTINCT tm.tm_id) AS occurances, t.value/occurances AS newValue FROM table t INNER JOIN table_map tm ON t.t_id = tm.tm_id GROUP BY tm.tm_id HAVING occurances > 1
作为那些熟悉SQl的人,这将行不通。但是,我对如何使它执行我所需要的或可能的情况感到困惑。我尝试在选择范围内进行选择,但无法弄清楚如何使其执行所需的操作。
在MySQL 5数据库上执行sql
也许您的意思是这样的:
SELECT t.t_id, t.occurrences, t.value / tm.occurrences AS newValue FROM table t INNER JOIN ( SELECT tm_id, COUNT(*) AS occurrences FROM table_map GROUP BY tm_id HAVING COUNT(*) > 1 ) tm ON t.t_id = tm.tm_id