【mysql】mysql用join实现Python中的in操作

背景:有两种数据表,一张device表,一张keyword表。

需要获取keyword表中在device表中存在device表的数据。做法就是先从device表中获取去重后所有的device数据;然后再通过device连接keyword表,取出keyword数据。

执行SQL如下:

with all_user as(
  select
    DISTINCT(device)
  from
    device_ttttt     #最好只获取一个字段
  where
    is_dubious = 0
    and os = 'android'
    and day <= '2019-03-29'
    and day >= '2019-02-29'
),
search as (
  select
    device,
    day,
    field ['keyword'].string_type
  from
    search_tttttt
  WHERE
    day <= '2019-03-29'
    and day >= '2019-02-29'
    and cmd = 801
    and device is not null
    and field ['keyword'].string_type is not null
    and (
      (
        field ['searchSource'].string_type = '3'
        AND field ['tabCode'].string_type = '1'
      )
      OR (
        field ['searchSource'].string_type = '0'
        AND field ['tabCode'].string_type = '1'
      )
      OR (
        field ['searchSource'].string_type = '6'
        AND field ['tabCode'].string_type = '1'
      )
    )
)
select
 *
from
  search
  left join all_user on search.device = all_user.device  limit 4;

先将获取的device数据放在中间表all_user中;

device存在all_user中的需要是唯一的,不然在join的时候出成倍的出现。

然后从search表中根据条件过滤取出keyword信息;

最后再根据device将两个表连接起来,取出需要的部分。

--------------

其实就是实现了拼音Python中in的操作,就是将search中device字段在device表中存在的取出来。

---------------

如果想要将获取的数据保存到一个单独的表中,需要先创建表,然后再insert到新表中:

在sql的头部,如果没有这个表就创建新表

create table if not exists test.test_table(`device` string, `keyword` string) PARTITIONED BY (day string) row format delimited fields terminated by '\t';
#创建表并以data作为区分

在末尾select语句前面加上insert语句:

insert overwrite table  test.test_table PARTITION(day='${YESTERDAY}')

这样就将查询的结果写入到新的数据表中

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一杯拿铁go

你的打赏是我更新最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值