【发布时间】:2014-10-07 07:48:53
【问题描述】:
我有 2 张桌子。 'table_a' 有更多列需要连接到一个结果:
'table_a' (time, col_1, col_2,....col_50) //'col_[1-50]' columns contains '$id'
example col_1 values: (1000, 1000, 1000, 2000, 2000, 2000)
example col_2 values: (2000, 2000, 1000, 2000, 3000, 3000) //etc
'table_b' ($id, name) //contains 'name' acording to '$id' from 'table_a'
example id,name values: (1000, John), (2000, Peter), (3000, Annie) //etc
我需要一些复杂的查询,就像我试着用文字为你写的一样(我知道,有趣的方式):
SELECT and JOIN or UNION col_[1-50] from table_a AS some 'result row'
AND count(*) duplicates/occurrences AS 'count', (DISTINCT after that)
(then GET 'name' FROM 'table_b' according to '$id')
and ORDER BY that 'count'
最终查询结果应该是这样的:
echo $query['count']." - ".$query['name']." - ".$query['id']."<br>";
6 - Peter - 2000
4 - John - 1000
2 - Annie - 3000
谢谢大家,我迷路了......
【问题讨论】:
-
在第一个代码部分的table_b“声明”的注释行中,它说“包含来自'table_a'的'$id'的'name'”。这到底是什么意思?我在任何地方都没有在 table_a 中看到 id 字段。
-
嗨,这意味着 col_1 - col_50 的值实际上是 id,就像你在比较两个表的示例值时看到的那样
标签: php mysql join count duplicates