如何将此查询的结果与自身交叉加入?
sql-fiddle:http://sqlfiddle.com/#!2/88f5e2/2
SELECT t.tid ,count(*) AS count
FROM taxonomy_index t
JOIN taxonomy_term_data td ON td.tid = t.tid
WHERE t.created > UNIX_TIMESTAMP() - 3 * 86400 AND td.vid = 1
GROUP BY t.tid
ORDER BY count DESC
LIMIT 0, 5;
查询结果为:
tid count
4429 6
2634 5
1703 4
1742 4
4468 4
我需要:
4429 4429 6
2634 4429 5
1703 4429 4
1742 4429 4
4468 4429 4
4429 2634 .
2634 2634 .
1703 2634 .
1742 2634
4468 2634
4429 1703
2634 1703
1703 1703
1742 1703
4468 1703
.... ....
答案 0 :(得分:0)
SELECT t_outer.tid as tid, t_inner.tid as tid_group, t_inner.count, t_inner.name
FROM taxonomy_index as t_outer
JOIN (
SELECT t.tid ,count(*) AS count, td.name, td.vid
FROM taxonomy_index t
JOIN taxonomy_term_data td ON td.tid = t.tid
WHERE t.created > 0 AND td.vid = 1
GROUP BY t.tid
) t_inner
WHERE t_outer.created > 0 AND t_inner.vid = 1
ORDER BY count DESC
LIMIT 0, 20;
的产率:
TID TID_GROUP COUNT NAME
4429 4429 3 A1
4468 4429 3 A1
4429 4429 3 A1
2634 4429 3 A1
4429 4429 3 A1
2634 4429 3 A1
4429 2634 2 B1
4468 2634 2 B1
4429 2634 2 B1
2634 2634 2 B1
4429 2634 2 B1
2634 2634 2 B1
2634 4468 1 C1
4429 4468 1 C1
4468 4468 1 C1
4429 4468 1 C1
2634 4468 1 C1
4429 4468 1 C1
http://sqlfiddle.com/#!2/88f5e2/29
信用到期的信用:count without group
更新:对不起,忘了约束外部数据。 更新2:我也忘记了tid列表