zoukankan      html  css  js  c++  java
  • sql 子查询 踩坑

      某个月黑风高的夜,手机的嗡嗡声提示收到一条短信,半睡半醒间打开手机查看,一条日志告警的信息使我睡意全无。

      掀被、起床、唤醒电脑,开始了一个程序员的填坑日常。

      有一个sql:

    select * from sms_sp_info where user_id = (select user_id  from app_user_info where account_name='13866666666' ) 

      sql中 user_id 字段 在app_user_info 表 并不存在。

      此时单独子查询

    select user_id from app_user_info where account_name='13866666666'

      报错

      但是整条查询语句会查询出sms_sp_info所有数据【这里说查询出sms_sp_info 所有数据还不准确,后期验证中,当 user_id 为null的值并不会查询出来】,

      原因

      子查询中并没有指定user_id 是app_user_info中的字段,所以整条sql执行,子查询user_id会获取父语句中的user_id字段。整条sql变成 select * from sms_sp_info where user_id = user_id ,获取所有sms_sp_info信息。

      优化sql 后

    select * from sms_sp_info sp 
    where sp.user_id = (select app.user_id from app_user_info app where app.account_name='13866666666')

      这样sql就会正常提示app_user_info中查找不到 user_id 。

      问题解决.....

      休眠电脑,上床,sleep ..[ 等等,好像忘了什么事情?--  果然,盖被子这件事情,还是需要自己来  _ . z Z  ]  

  • 相关阅读:
    (Good Bye 2019) Codeforces 1270B Interesting Subarray
    (Good Bye 2019) Codeforces 1270A Card Game
    Codeforces 1283D Christmas Trees(BFS)
    Codeforces 1283C Friends and Gifts
    Codeforces 1283B Candies Division
    1095 Cars on Campus (30)
    1080 Graduate Admission (30)
    1099 Build A Binary Search Tree (30)
    1018 Public Bike Management (30)
    1087 All Roads Lead to Rome (30)
  • 原文地址:https://www.cnblogs.com/wgy1/p/10542288.html
Copyright © 2011-2022 走看看