zoukankan      html  css  js  c++  java
  • 经典笛卡尔积SQL

    经典笛卡尔积SQL:

    下面的SQL会造成笛卡尔积:

    insert into tydic.temp_0731 select a.user_id,a.province_code,b.attr_code from tydic.pline_user_tmp a,tydic.pline_attr_tmp b
    where a.user_id = b.user_id
    and (a.service_id = '80000009'  and b.attr_code in ('10000500',
    '10000501',
    '10000502',
    '10000503'
    )) or (a.service_id='80000026' and b.attr_code in ('10000105',
    '10000112',
    '10000128',
    '10000129'
    )) or (a.service_id='80000014' and b.attr_code in ('10000500',
    '10000501',
    '10000502',
    '10001302'
    )) or (a.service_id='80000015' and b.attr_code in ('10000501',
    '10000502',
    '10001302',
    '10001303'
    )) or (a.service_id='80000029' and b.attr_code in ('10000500',
    '10000507',
    '10000508',
    '10000501'
    )) or (a.service_id='80000032' and b.attr_code in (
    '10000500',
    '10001300',
    '10000507')) or (a.service_id='80000030' and b.attr_code in (
    '10001025',
    '10001026',
    '10001022'
    )) or (a.service_id='80000033' and b.attr_code in (
    '10001020',
    '10000502',
    '10000505')) or (a.service_id='80000016' and b.attr_code in (
    '10001010',
    '10001011',
    '10001013'
    )) or (a.service_id='80000007' and b.attr_code in (
    '10001007',
    '10001008',
    '10001009'
    )) or (a.service_id='80000023' and b.attr_code in (
    '10001001',
    '10001005',
    '10001006'
    )) or (a.service_id='80000034' and b.attr_code in (
    '10001001',
    '10001002',
    '10000100'
    ))  or (a.service_id='80000017' and b.attr_code in (
    '10001101',
    '10001102',
    '10001103'
    ));
    commit;

    经过下面改造,不会出现笛卡尔积:

    insert into tydic.temp_0731 select a.user_id,a.province_code,b.attr_code from tydic.pline_user_tmp a,tydic.pline_attr_tmp b
    where a.user_id = b.user_id
    and( (a.service_id = '80000009'  and b.attr_code in ('10000500',
    '10000501',
    '10000502',
    '10000503'
    )) or (a.service_id='80000026' and b.attr_code in ('10000105',
    '10000112',
    '10000128',
    '10000129'
    )) or (a.service_id='80000014' and b.attr_code in ('10000500',
    '10000501',
    '10000502',
    '10001302'
    )) or (a.service_id='80000015' and b.attr_code in ('10000501',
    '10000502',
    '10001302',
    '10001303'
    )) or (a.service_id='80000029' and b.attr_code in ('10000500',
    '10000507',
    '10000508',
    '10000501'
    )) or (a.service_id='80000032' and b.attr_code in (
    '10000500',
    '10001300',
    '10000507')) or (a.service_id='80000030' and b.attr_code in (
    '10001025',
    '10001026',
    '10001022'
    )) or (a.service_id='80000033' and b.attr_code in (
    '10001020',
    '10000502',
    '10000505')) or (a.service_id='80000016' and b.attr_code in (
    '10001010',
    '10001011',
    '10001013'
    )) or (a.service_id='80000007' and b.attr_code in (
    '10001007',
    '10001008',
    '10001009'
    )) or (a.service_id='80000023' and b.attr_code in (
    '10001001',
    '10001005',
    '10001006'
    )) or (a.service_id='80000034' and b.attr_code in (
    '10001001',
    '10001002',
    '10000100'
    ))  or (a.service_id='80000017' and b.attr_code in (
    '10001101',
    '10001102',
    '10001103'
    )));
    commit;

  • 相关阅读:
    小朋友排队--第五届蓝桥杯
    Spring IOC源代码具体解释之整体结构
    Libimseti推荐系统
    Codeforces Round #277.5 (Div. 2)(C题)
    数据库经常使用函数
    Command terminated by signal 11
    winform程序公布后,client下载报错“您的 Web 浏览器设置不同意执行未签名的应用程序”
    Cocos2d-x学习笔记(四) 布景层的加入移除
    FMSC 使用理解
    将浮点数保持几位小数,尾数舍入的Format函数
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3241341.html
Copyright © 2011-2022 走看看