zoukankan      html  css  js  c++  java
  • A表有字段a,B表有字段b,字段a中包含字段b,如何关联查询

    比如 AA表如
    xh  shengccj
    1   河南羚锐生物药业                                  
    2   四川蜀中制药有限公司                              
    3   海口奇力制药

    BB表 
    xh  shengccj
    1   河南羚锐生物
    1   河南中杰药业                                     
    2   四川蜀中制药
    2   海口奇力制药
    3   河南中杰药业 
    我想查询 的条件为 A表xh=b表xh   a表中shengccj 必须在b表中shengccj的
    结果为
    xh shengccj
    1   河南羚锐生物药业                                  
    2   四川蜀中制药有限公司   

    CREATE TABLE AA 
    (
        xh INT,
        shengccj VARCHAR(100)
    )
     
     
    INSERT INTO AA
    SELECT 1, '河南羚锐生物药业'
    UNION ALL 
    SELECT 2, '四川蜀中制药有限公司'
    UNION ALL 
    SELECT 3, '海口奇力制药'
     
    CREATE TABLE BB 
    (
        xh INT,
        shengccj VARCHAR(100)
    )
    INSERT INTO BB
    SELECT 1, '河南羚锐生物'
    UNION ALL 
    SELECT 1, '河南中杰药业'
    UNION ALL 
    SELECT 2, '四川蜀中制药'
    UNION ALL 
    SELECT 2, '海口奇力制药'
    UNION ALL 
    SELECT 3, '河南中杰药业'
    GO
    --开始查询
    --1
    select a.xh,a.shengccj from AA a left join BB b  on  a.xh=b.xh  where   b.shengccj like substring(a.shengccj,1,6)
    --2
    select a.xh,a.shengccj from  AA a ,BB b where a.xh=b.xh and b.shengccj like left(a.shengccj,6)
    --3
    select a.xh,a.shengccj from AA a,BB b where a.xh=b.xh and a.shengccj like '%'+b.shengccj+'%' --这个也可以出结果啊
    /*
    -----------------------------------------------------------------------
    xh    shengccj
    1     河南羚锐生物药业   
    2     四川蜀中制药有限公司
     
     
     
    (2 行受影响)
    ------------------------------------------------------------------------
    */
  • 相关阅读:
    js 中将 ‘Thu, 20 Feb 2020 14:21:15 GMT’ 转成 ‘yyyy-mm-dd hh:dd:ss’
    mysql 操作
    flask+bootstrap 页面查询、分页
    bootstrap静态框,调后端接口结果展示
    UndefinedError: 'int' is undefined 解决
    robot framework web自动化-登录
    robot framework 执行web自动化一次后没有log日志,重启后执行一次后也是
    robot framework 数据库增删改查
    robot framework get、post 接口调用
    PCM-FTL
  • 原文地址:https://www.cnblogs.com/albert-/p/14690693.html
Copyright © 2011-2022 走看看