gorm踩坑笔记1

dlutzhangyi ·
``` // Get first record, order by primary key db.First(&user) //// SELECT * FROM users ORDER BY id LIMIT 1; // Get one record, no specified order db.Take(&user) //// SELECT * FROM users LIMIT 1; // Get last record, order by primary key db.Last(&user) //// SELECT * FROM users ORDER BY id DESC LIMIT 1; // Get all records db.Find(&users) //// SELECT * FROM users; // Get record with primary key (only works for integer primary key) db.First(&user, 10) //// SELECT * FROM users WHERE id = 10; ``` `First` 本身就会根据 id 进行排序, 第二个参数是可以指定查询 ID,你这个场景用 `Take` 就可以了
#1