Home  >  Q&A  >  body text

数据库 - mysql联表去重查询

现有两张表:

  1. 分类表A :

    id·······cate_name
    1········mysql
    2········php
    3·······java
    4·······ios
  2. 文章表B :

    id····cateid···content····time
    1·······1·······xxx······时间戳
    2·······2·······xxx······时间戳
    3·······3·······xxx······时间戳
    4·······1·······xxx······时间戳
    5·······1·······xxx······时间戳
    6·······3·······xxx······时间戳

    现在想 select * from B left join A on B.cateid = A.id,当然这样会有重复的分类文章会查出来。怎么写SQL 能从B 表关联查询出每个分类下的最新发表的文章和对应的文章分类呢?求 具体的查询语句

大家讲道理大家讲道理2743 days ago717

reply all(2)I'll reply

  • 高洛峰

    高洛峰2017-04-17 14:45:47

    You must first group and query the articles with the largest timestamps in each category, and then jointly query with A

    reply
    0
  • 黄舟

    黄舟2017-04-17 14:45:47

    select tb.*,A.cate_name from  (select * from B order by cateid,time desc) tb left join A on tb.cateid = A.id group by tb.cateid

    reply
    0
  • Cancelreply