• 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 行受影响)
    ------------------------------------------------------------------------
    */
  • 相关阅读:
    设计模式-行为型模式,python备忘录模式
    设计模式-行为型模式,python 中介者模式
    python 迭代器模式
    python对象池模式
    设计模式-结构型模式,python组合模式
    设计模式-结构型模式,python桥接模式
    python concurrent.futures.Threadpoolexcutor的有界队列和无界队列
    python加快数据处理的方法
    面向切面编程AOP,一些通用装饰器
    supervisor的command执行两条命令
  • 原文地址:https://www.cnblogs.com/albert-/p/14690693.html
Copyright © 2020-2023  润新知