您好我正在尝试在mysql视图中循环实现行自动编号。 我确实从Pure-SQL Technique for Auto-Numbering Rows in Result Set开始并让它发挥作用。然后我尝试调整它并在那里得到ALMOST ......现在我是stuMainmped
该视图列出了狗(CaneID)及其后代(AvoID)。我想要实现的是每次主狗(CaneID)改变时重新编号,这样我就可以让每个上升者属于一个从1开始的狗号。
到目前为止它仅适用于第一只狗,以下仍按顺序编号但不从1开始。
这是我的查询查询:
SELECT
`avi1`.`CaneID`,
`avi1`.`Cane`,
`avi1`.`PedPos`,
`avi1`.`AvoID`,
`avi1`.`Avo`,
`avi1`.`Ripetuto`,
(
SELECT
count(0)
FROM
`v_avi_ripetuti` `Avi2`
WHERE
(
(`avi2`.`Ripetuto` > 1)
AND (`avi2`.`CaneID` <= `avi1`.`CaneID`)
AND (`avi2`.`AvoID` <= `avi1`.`AvoID`)
)
ORDER BY
`avi2`.`CaneID`,
`avi2`.`AvoID`
) AS `RowNumber`
FROM
`v_avi_ripetuti` `Avi1`
WHERE
(`avi1`.`Ripetuto` > 1)
ORDER BY
`avi1`.`CaneID`,
`avi1`.`AvoID`
我现在得到的是:
CaneID Cane AvoID Avo RowNumber
---------------------------------------------------------------------
2 Antigua 472 Anika v. Stammhaus Eike 1
2 Antigua 527 Britta v.d. Römerlinde 2
2 Antigua 642 Ares v. Nettenberg 3
2 Antigua 657 Carmen v. Westfalenzwinger 4
2 Antigua 658 Leu v. Stammhaus Eike 5
2 Antigua 684 Chipsy v. Theresienhof 6
2 Antigua 1662 Astrit v.d. Burrlinde 7
-----------------------> New Main dog<-------------------------------
3 Ambra Gaya 472 Anika v. Stammhaus Eike 2
3 Ambra Gaya 657 Carmen v. Westfalenzwinger 6
3 Ambra Gaya 658 Leu v. Stammhaus Eike 8
3 Ambra Gaya 684 Chipsy v. Theresienhof 10
3 Ambra Gaya 718 Leo v. guten Löwen 11
3 Ambra Gaya 1022 Zara v. Ratibor u. Corvey 12
3 Ambra Gaya 1024 Ruth v. Ratibor u. Corvey 13
3 Ambra Gaya 1050 Dunja v. Hofoldinger Forst 14
3 Ambra Gaya 1200 Icksle v. Ratibor u. Corvey 15
3 Ambra Gaya 1227 Troldegaards Tanja 16
3 Ambra Gaya 1228 Anemone v. Ratibor u. Corvey 17
-----------------------> New Main dog<-------------------------------
3371 Always Habit 600 Absinthe v. Kallenberg 4
3371 Always Habit 750 Mathoaka's Relaxing Boy 13
3371 Always Habit 2560 Leijonamielen Luvaton Lyyli 21
我是mysql的新手(一周前安装它)并且非常努力地让访问权限落后......请耐心等待。
答案 0 :(得分:1)
我猜你想要按照结果排序,首先是CaneId
,然后是AvoId
。如果是这样,正确的子查询是:
(SELECT count(*)
FROM v_avi_ripetuti` `Avi2`
WHERE `avi2`.`Ripetuto` > 1 AND
(`avi2`.`CaneID` < `avi1`.`CaneID` or
`avi2`.`CaneID` = `avi1`.`CaneID` and avi2`.`AvoID` <= `avi1`.`AvoID`
)
) AS `RowNumber`
除了修复where
子句之外,我还删除了order by
,因为它与子查询无关。我还将count(0)
更改为count(*)
。这些是等价的,但count(0)
看起来很尴尬。
答案 1 :(得分:1)
我知道!!!!!!! 我使用函数SUM()而不是COUNT()。 这是我的mysql查看代码,万一其他人可以使用它:
SELECT
`avi1`.`CaneID`,
`avi1`.`Cane`,
`avi1`.`PedPos`,
`avi1`.`AvoID`,
`avi1`.`Avo`,
`avi1`.`Ripetuto`,
(
SELECT
sum(
IF (
(
(
`avi2`.`CaneID` = `avi1`.`CaneID`
)
AND (`avi2`.`AvoID` <= `avi1`.`AvoID`)
),
1,
0
)
)
FROM
`v_avi_ripetuti` `Avi2`
WHERE
(`avi2`.`Ripetuto` > 1)
) AS `NumRiga`
FROM
`v_avi_ripetuti` `Avi1`
WHERE
(`avi1`.`Ripetuto` > 1)
这给了我想要的结果:
CaneID Cane AvoID Avo RowNumber
---------------------------------------------------------------------
2 Antigua 472 Anika v. Stammhaus Eike 1
2 Antigua 527 Britta v.d. Römerlinde 2
2 Antigua 642 Ares v. Nettenberg 3
2 Antigua 657 Carmen v. Westfalenzwinger 4
2 Antigua 658 Leu v. Stammhaus Eike 5
2 Antigua 684 Chipsy v. Theresienhof 6
2 Antigua 1662 Astrit v.d. Burrlinde 7
-----------------------> New Main dog<-------------------------------
3 Ambra Gaya 472 Anika v. Stammhaus Eike 1
3 Ambra Gaya 657 Carmen v. Westfalenzwinger 2
3 Ambra Gaya 658 Leu v. Stammhaus Eike 3
3 Ambra Gaya 684 Chipsy v. Theresienhof 4
3 Ambra Gaya 718 Leo v. guten Löwen 5
3 Ambra Gaya 1022 Zara v. Ratibor u. Corvey 6
3 Ambra Gaya 1024 Ruth v. Ratibor u. Corvey 7
3 Ambra Gaya 1050 Dunja v. Hofoldinger Forst 8
3 Ambra Gaya 1200 Icksle v. Ratibor u. Corvey 9
3 Ambra Gaya 1227 Troldegaards Tanja 10
3 Ambra Gaya 1228 Anemone v. Ratibor u. Corvey 11
-----------------------> New Main dog<-------------------------------
3371 Always Habit 600 Absinthe v. Kallenberg 1
3371 Always Habit 750 Mathoaka's Relaxing Boy 2
3371 Always Habit 2560 Leijonamielen Luvaton Lyyli 3