SELECT
article.*,
person.name as person_name
FROM article
LEFTJOIN person ON person.id=article.person_id
WHERE article.type =0AND article.age IN (18,20)
SELECT
o.*,
d.department_name,
(SELECTSum(so.goods_fee) AS task_detail_target_completed_tem
FROM sale_order so
WHERE so.merchant_id ='356469725829664768'AND so.create_date BETWEEN (20230127) AND (20230212)
AND so.delete_state =2AND so.department_id = o.department_id
) AS task_detail_target_completed
FROM task_detail o
LEFTJOIN department d ON d.department_id=o.department_id
WHERE o.merchant_id ='356469725829664768'AND o.task_id ='356469725972271104768'
<mappernamespace="xxx.mapper.UserMapper"><insertid="insertUser"parameterType="UserEntity">
insert into user (id,name,mobile)
values (#{id},#{name},#{mobile})
</insert><updateid="updateUser"parameterType="UserEntity">
update user set
name = #{name},
mobile = #{mobile}
where id = #{id}
</update><deleteid="deleteUser">
delete from user where id = #{id}
</delete><selectid="selectUsers"resultType="UserVO">
select u.*, (select count(*) from article a where a.uid=u.id) as article_count
from user u
where u.id = #{id}
</select></mapper>
var id = 1;
var query = database.Posts
.Join(database.Post_Metas,
post => post.ID,
meta => meta.Post_ID,
(post, meta) => new { Post = post, Meta = meta }
.Where(postAndMeta => postAndMeta.Post.ID == id);
$repository = $this->getDoctrine()->getRepository('AppBundle:Product');
// query for a single product by its primary key (usually "id")// 通过主键(通常是id)查询一件产品$product = $repository->find($productId);
// dynamic method names to find a single product based on a column value// 动态方法名称,基于字段的值来找到一件产品$product = $repository->findOneById($productId);
$product = $repository->findOneByName('Keyboard');
// query for multiple products matching the given name, ordered by price// 查询多件产品,要匹配给定的名称和价格$products = $repository->findBy(
array('name' => 'Keyboard'),
array('price' => 'ASC')
type User struct {
Id int
Age inttype Order struct {
UserId int
FinishedAt *time.Time
query := db.Table("order").
Select("MAX(order.finished_at) as latest").
Joins("left join user user on order.user_id = user.id").
Where("user.age > ?", 18).
Group("order.user_id")
db.Model(&Order{}).
Joins("join (?) q on order.finished_at = q.latest", query).
Scan(&results)
这是一个嵌套查询,虽然定义了模型,但是查询的时候并没有使用模型的属性,而是输入硬编码
很显然,它会产生SQL编写难题
另外,是先设计模型,属于 code first 模式
go体系下的gorm gen
gorm gen 是 gorm 团队开发的另一款产品,和mybaits下的Mybatis3 Dynamic Sql比较像
u := query.User
err := u.WithContext(ctx).
Select(u.Name, u.Age.Sum().As("total")).
Group(u.Name).
Having(u.Name.Eq("group")).
Scan(&users)
// SELECT name, sum(age) as total FROM `users` GROUP BY `name` HAVING name = "group"
这是一个分组查询,定义了模型,也使用了模型的属性。
但是呢,它需要使用工具生成额外的支持代码,并且需要先定义数据表
很显然,它不会产生SQL编写难题
另外,它是先设计表,属于 database first 模式
go体系下的ent
ent 是 facebook公司开发的Orm产品,与 gorm gen 有相通,也有不同
相同点在于,都是利用工具生成实体与数据表字段的映射关系
不同点在于gorm gen先有表和字段,然后生成实体
ent是没有表和字段,你自己手动配置,配置完了一起生成实体和建表
接下来,看一眼ent生成的映射关系
const (
// Label holds the string label denoting the user type in the database.
Label = "user"// FieldID holds the string denoting the id field in the database.
FieldID = "id"// FieldName holds the string denoting the name field in the database.
FieldName = "name"// FieldAge holds the string denoting the age field in the database.
FieldAge = "age"// FieldAddress holds the string denoting the address field in the database.
FieldAddress = "address"// Table holds the table name of the user in the database.
Table = "users"
有了映射关系,使用起来就比较简单了
u, err := client.User.
Query().
Where(user.Name("realcp")).
Only(ctx)
注意,这里没有硬编码
它需要使用工具生成额外的支持代码,并且需要先配置表结构
很显然,它不会产生SQL编写难题
另外,它属于先设计表,属于 database first 模式
go体系下的aorm
aorm 是我自己开发的orm库,吸取了ef core 的一些优点,比较核心的步骤如下
和大多数orm一样,需要先建立模型,比如
type Person struct {
Id null.Int `aorm:"primary;auto_increment" json:"id"`
Name null.String `aorm:"size:100;not null;comment:名字" json:"name"`
Sex null.Bool `aorm:"index;comment:性别" json:"sex"`
Age null.Int `aorm:"index;comment:年龄" json:"age"`
Type null.Int `aorm:"index;comment:类型" json:"type"`
CreateTime null.Time `aorm:"comment:创建时间" json:"createTime"`
Money null.Float `aorm:"comment:金额" json:"money"`
Test null.Float `aorm:"type:double;comment:测试" json:"test"`
然后实例化它,并且保存起来
//Instantiation the structvar person = Person{}
//Store the struct object
aorm.Store(&person)
然后即可使用
var personItem Person
err := aorm.Db(db).Table(&person).WhereEq(&person.Id, 1).OrderBy(&person.Id, builder.Desc).GetOne(&personItem)
if err != nil {
fmt.Println(err.Error())