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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/w417950004/article/details/88952637

背景:有两种数据表,一张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}')

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

猜你喜欢

转载自blog.csdn.net/w417950004/article/details/88952637