美文网首页
中间表三表联查

中间表三表联查

作者: OLDBIG9 | 来源:发表于2019-10-07 22:01 被阅读0次

示例表:

CREATE TABLE IF NOT EXISTS authors (
    `author_id` INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
    `name` VARCHAR(64) UNIQUE NOT NULL DEFAULT ''
)  ENGINE INNODB COMMENT '作者表';

create table if not exists articles (
    `article_id` int unsigned primary key auto_increment,
    `title` varchar(64) unique not null default '',
    `created_at` timestamp not null default CURRENT_TIMESTAMP,
    `updated_at` timestamp not null default CURRENT_TIMESTAMP
)engine innodb comment '文章表';

create table if not exists author_article (
    `article_id` int unsigned not null,
    `author_id` int unsigned not null,
    primary key (`author_id`,`article_id`)
)engine innodb comment '作者文章关联表';

按照文章的最新发布时间查找对应作者列表

SELECT 
    au.author_id AS author_id, au.name AS name
FROM
    (authors AS au
    LEFT JOIN author_article AS aa ON au.author_id = aa.author_id)
        LEFT JOIN
    articles AS ar ON aa.article_id = ar.article_id
ORDER BY ar.created_at DESC

相关文章

  • 中间表三表联查

    示例表: 按照文章的最新发布时间查找对应作者列表

  • 中间表三表联查

    示例表: 按照文章的最新发布时间查找对应作者列表

  • MyBatis多表查询(3)

    两表联查(多对多) 1、创建一个功能菜单(menu)表、角色(role)表和中间(middle)表 2、创建Men...

  • 2021-08-29

    数据模型: 插入数据: 查询数据: 两表,连查询研究: 单表查询研究: 结果; 三表联查 结果: 项目思路:"""...

  • Oracle数据库——多表查询

    一、关联查询 1.关联查询select 表1的字段名,表2的字段名 from 表1 [别名1],表2 [别名2] ...

  • 三表联查技巧

    select * fromtable1 t1,table2 t2,table3 t3where t1.id=t2....

  • Mysql 三表联查

    select * from (BO_EU_QA_ACTION t1 left join BO_EU_ACCESS...

  • mysql三表联查

    A表 客户资料表 用到的值id,custid,nameB表 客户设备表 custid,deviceid...

  • PHP 三表联查

    $list_info = Db::table('first_second_map') ->field('map.*...

  • 关联关系查询(第一讲)

    关联查询 当查询内容涉及到具有关联关系的多个表时,就需要使用关联查询。根据表与表之间的关联关系的不同,关联查询分为...

网友评论

      本文标题:中间表三表联查

      本文链接:https://www.haomeiwen.com/subject/bxivuctx.html