เพื่อใช้เป็น Primary Key ไว้ Replace
REPLACE INTO mb_HbA1c
SELECT * FROM (
SELECT (@cnt := @cnt + 1) AS ID, a.VISIT_ID, b.HN ,DATE_FORMAT(a.LREQ_DT,'%Y-%m-%d') AS DATE_SERV , LAB_RESULT,SUBSTR(a.lab_result,7,4) as A1c, a.STAFF_ID, d.TOWN_ID,
FLOOR(DATEDIFF(NOW() ,d.BIRTHDATE)/365.25) AS AGE,
SELECT
(@cnt := @cnt + 1) AS rowNumber,
t.rowID
FROM myTable AS t
CROSS JOIN (SELECT @cnt := 0) AS dummy
WHERE t.CategoryID = 1
ORDER BY t.rowID ;
Result
| ROWNUMBER | ROWID |
---------------------
| 1 | 1 |
| 2 | 25 |
| 3 | 33 |
| 4 | 150 |