zoukankan      html  css  js  c++  java
  • 一道sql面试题

    问题描述:
    两个表a,b
    a中字段为:id,name
    b中字段为:id,email,address
    查询语句:select a.*,b.* from a left join b on a.id=b.id
    结果为:

    id          name                                               sex                                                id          address                                            email
    ----------- -------------------------------------------------- -------------------------------------------------- ----------- -------------------------------------------------- -
    1           zy                                                 male                                               1           beijing                                            zyip@qq.com
    1           zy                                                 male                                               1           American                                           zyip@msn.com
    2           zk                                                 male                                               2           beijing                                            zk@qq.com
    2           zk                                                 male                                               2           nanjing                                            zk@sina.com.cn

    (4 row(s) affected) 

     问题,如果在left join语句中只取出与附表第一条记录联合的结果如何做?即主表数据只和附表第一条联合,其余忽略,例如下列结果:
    id          name                                               sex                                                id          address                                            email
    ----------- -------------------------------------------------- -------------------------------------------------- ----------- -------------------------------------------------- ---------------
    1           zy                                                 male                                               1           beijing                                            zyip@qq.com
    2           zk                                                 male                                               2           beijing                                            zk@qq.com



     

  • 相关阅读:
    Coalesce (MS SQL Server)——取指定内容(列)中第一个不为空的值
    SQL 函数NULLIF、NULL、ISNULL、COALESCE、IIF
    oracle 分组后取每组第一条数据
    oracle判断是否包含字符串的方法
    Oracle 查询字段不包含多个字符串方法
    [oracle] to_date() 与 to_char() 日期和字符串转换
    Oracle中保留两位小数
    Oracle 树操作、递归查询(select…start with…connect by…prior)
    联合查询更新表数据
    WCF 之 生成元数据和代理
  • 原文地址:https://www.cnblogs.com/zyip/p/1549927.html
Copyright © 2011-2022 走看看