我的表有4个标签链接到每个id。我想选择该值的位置(排名)(tag_name或tag_id)以及值(tag_name或tag_id)在同一位置(排名)中显示的次数。
这是mysql中的样子:
> +--------+------------+--------+------+ > | id | tag_name | tag_id | rank | > +--------+------------+--------+------+ > | 2345 | cookie | 2 | 1 | > | 2345 | bar | 1 | 2 | > | 2345 | cereal | 3 | 3 | > | 2345 | milk | 4 | 4 | > | 2346 | cereal | 3 | 1 | > | 2346 | milk | 4 | 2 | > | 2346 | cookie | 2 | 3 | > | 2346 | hot dogs | 5 | 4 | > | 2347 | chocolate | 6 | 1 | > | 2347 | bar | 1 | 2 | > +--------+------------+--------+------+
以下是我当前的代码:
SELECT m.*, tr.tag_id, t.tag_name, @rownum:=@rownum + 1 AS rank
FROM meals AS m
RIGHT JOIN tags_rel AS tr ON tr.meal_id = m.id
JOIN tags AS t ON tr.tag_id = t.id
JOIN (SELECT @rownum:=0) AS r
ORDER BY m.id DESC
请记住,我实际上正在使用分页,所以我的ORDER BY实际上看起来像这样:
ORDER BY id DESC LIMIT $start_from, 12
如果我选择tag_name = bar或tag_id = 1且rank = 2,我应该得到以下结果。另外,我想为该值返回行数。
> +--------+------------+--------+------+ > | id | tag_name | tag_id | rank | > +--------+------------+--------+------+ > | 2345 | bar | 1 | 2 | > | 2347 | bar | 1 | 2 | > +--------+------------+--------+------+
谢谢!
答案 0 :(得分:2)
SELECT tag_name, tag_id, rank, COUNT(tag_id) AS rankcount
FROM table
GROUP BY tag_id, rank;
结果:
+-----------+--------+------+-----------+
| tag_name | tag_id | rank | rankcount |
+-----------+--------+------+-----------+
| bar | 1 | 2 | 2 |
| cookie | 2 | 1 | 1 |
| cookie | 2 | 3 | 1 |
| cereal | 3 | 1 | 1 |
| cereal | 3 | 3 | 1 |
| milk | 4 | 2 | 1 |
| milk | 4 | 4 | 1 |
| hot dogs | 5 | 4 | 1 |
| chocolate | 6 | 1 | 1 |
+-----------+--------+------+-----------+
9 rows in set (0.00 sec)
如果你希望你的原始表增强了一些行数(虽然这看起来很无意义,因为每个sql结果都返回行数),你必须做一个子查询:
SELECT id, tag_name, tag_id as t_id, rank,
(SELECT count(tag_id) FROM table WHERE tag_id = t_id) as subqueryCount
FROM table;
+------+-----------+------+------+---------------+
| id | tag_name | t_id | rank | subqueryCount |
+------+-----------+------+------+---------------+
| 2345 | cookie | 2 | 1 | 2 |
| 2345 | bar | 1 | 2 | 2 |
| 2345 | cereal | 3 | 3 | 2 |
| 2345 | milk | 4 | 4 | 2 |
| 2346 | cereal | 3 | 1 | 2 |
| 2346 | milk | 4 | 2 | 2 |
| 2346 | cookie | 2 | 3 | 2 |
| 2346 | hot dogs | 5 | 4 | 1 |
| 2347 | chocolate | 6 | 1 | 1 |
| 2347 | bar | 1 | 2 | 2 |
+------+-----------+------+------+---------------+
10 rows in set (0.00 sec)
扩展该查询将产生您想要的结果,由插入行数的列扩展(这对IMO来说仍然相当无意义)。
SELECT id, tag_name, tag_id as t_id, rank,
(SELECT count(tag_id) FROM table WHERE tag_id = t_id) as subqueryCount
FROM table
WHERE tag_id = 1;
结果
+------+----------+------+------+---------------+
| id | tag_name | t_id | rank | subqueryCount |
+------+----------+------+------+---------------+
| 2345 | bar | 1 | 2 | 2 |
| 2347 | bar | 1 | 2 | 2 |
+------+----------+------+------+---------------+
2 rows in set (0.00 sec)
答案 1 :(得分:1)
怎么样:
SELECT DISTINCT meal_id as MealId, t2name as TagName, t2id as TagId, Rank
FROM
(SELECT t1id as t2id, t1name as t2name, rnk as rnk2, count(*) as Rank
FROM
tags_rel AS r,
(SELECT m1id, t1id, t1name,
@rnk := CASE WHEN @id <> m1id THEN 1 ELSE @rnk + 1 END AS rnk, @id := m1id as idx
FROM
tags_rel AS r,
(SELECT @id := 0) idx,
(SELECT @rnk := 0) rxx,
(SELECT m.id AS m1id, t.id AS t1id, name AS t1name
FROM tags_rel r
JOIN (meals AS m, tags AS t ) ON ( m.id = r.meal_id AND t.id = r.tag_id )
) AS rn
WHERE r.tag_id = t1id AND r.meal_id = m1id
ORDER BY m1id, t1name
) AS rno
WHERE r.tag_id = t1id AND r.meal_id = m1id
group BY t1id, t1name, rnk
) as rn2
join (meals, tags_rel) on (meals.id = tags_rel.meal_id and tags_rel.tag_id = t2id)
ORDER BY meal_id, t2id
导致:
2345 bar 1 2
2345 cookie 2 1
2345 cereal 3 1
2345 milk 4 2
2346 cookie 2 1
2346 cereal 3 1
2346 milk 4 2
2346 hotdog 5 1
2347 bar 1 2
2347 chocolad 6 1