测试实体类(表结构) public class User { public int user_id { get; set; } public string user_name { get; set; } } public class Book { public int book_id { get; set; } public string book_name { get; set; } public int user_id { get; set; } } //一对多关系 public class…
1. SELECT语句的子查询 语法: SELECT ... FROM (subquery) AS name ... 先创建一个表: CREATE TABLE t1 (s1 INT, s2 CHAR(5), s3 FLOAT);INSERT INTO t1 VALUES (1,'1',1.0);INSERT INTO t1 VALUES (2,'2',2.0); 我们就可以进行以下的嵌套查询了:SELECT sb1,sb2,sb3 FROM (SELECT s1 AS sb1…
多表查询是模型层的重要功能之一, Django提供了一套基于关联字段独特的解决方案. ForeignKey 来自Django官方文档的模型示例: from django.db import models class Blog(models.Model): name = models.CharField(max_length=100) tagline = models.TextField() class Author(models.Model): name = models.CharField(m…