MySQL中简单的select子查询中exsits的伪代码运作方式

以下只是我所做的一些简单的测试验证所得的结果,若有不对请指正。

语句格式

select 显示的列 
from1
where exists (
    select * from2 where 条件表达式
)

伪代码

可以看到,表1在外循环,表2在内循环,只要在内循环中至少有一次满足条件表达式,就输出表1的该行

rst = []                      # 存放select结果的列表,原为空
for i in1:                 # i是表1的每一行
    flag = False
    for j in2:             # j是表2的每一行
        if 条件表达式:        # 条件表达式是由i、j作比较组成的
            # 只要有一个条件满足,那么这个表1的行记录就会被加入结果列表
            rst.push_back(i);
            break;

MySQL语句举例

例如以下的MySQL语句:

select *
from tb_a 
where exists (
    select * from tb_b where tb_a.id = tb_b.id
);

它的伪代码如下:

rst = []
for i in tb_a:
    for j in tb_b:
        if i.id == j.id:
            rst.push_back(i);
            break;

验证过程

我在测试中使用了下面两张表:

tb_a

empidnameage
A101张三40
A102李四28
A103王五20
A104赵六19
A105杨甲19
A106张三17
A108张B31
A107杨A19

tb_b

empidsalesmonth
A1031014
A102545
A1041814
A1011844
A103175
A1013005
A1022056
A104935
A103126
A107876
A1003053

我使用了四个测试用例,它们的mysql代码如下(它们仅仅是条件表达式不同):

select * from tb_a
where exists (
    select * from tb_b where tb_a.empid = tb_b.empid
);

select * from tb_a
where exists (
    select * from tb_b where tb_a.empid = 'A101'
);

select * from tb_a
where exists (
    select * from tb_b where tb_b.sales > 305
);

select * from tb_a
where exists (
    select * from tb_b where tb_b.sales > 304
);

下面是我用来做测试写的python代码,而上面四个实例的对象条件表达式就在代码的注释中:

import pandas as pd

# 仅仅用于整齐输出
def show_table(lst, table):
    tmp = pd.DataFrame(lst, columns=table.columns)
    if not tmp.empty:
        print(tmp)
    else:
        print("No data")

# 模仿select-exists运作的伪代码
def select_exists(table1, table2):
    rst = []
    for _, i in table1.iterrows():
        for _, j in table2.iterrows():
            # 条件表达式
            if i['empid'] == j['empid']: # tb_a.empid = tb_b.empid
            # if i['empid'] == 'A101':   # tb_a.empid = 'A101'
            # if j['sales'] > 304:       # tb_b.sales > 305
            # if j['sales'] > 305:       # tb_b.sales > 304
                # 只要有一个条件满足,那么这个表1的行记录就会被加入结果列表
                rst.append(tuple(i))
                break;
    show_table(rst, table1)


if __name__ == "__main__":

    # 表1
    tb_a = pd.DataFrame(
        columns = [ 'empid', 'name', 'age'],
        data = [
            [ 'A101', '张三', 40],
            [ 'A102', '李四', 28],
            [ 'A103', '王五', 20],
            [ 'A104', '赵六', 19],
            [ 'A105', '杨甲', 19],
            [ 'A106', '张三', 17],
            [ 'A108', '张B', 31],
            [ 'A107', '杨A', 19]
        ]
    )

    # 表2
    tb_b = pd.DataFrame(
        columns=['empid', 'sales', 'month'],
        data = [
            [ 'A103', 101, 4 ],
            [ 'A102',  54, 5 ],
            [ 'A104', 181, 4 ],
            [ 'A101', 184, 4 ],
            [ 'A103',  17, 5 ],
            [ 'A101', 300, 5 ],
            [ 'A102', 205, 6 ],
            [ 'A104',  93, 5 ],
            [ 'A103',  12, 6 ],
            [ 'A107',  87, 6 ],
            [ 'A100', 305, 3 ]
        ]
    )

    select_exists(tb_a, tb_b)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值