zoukankan      html  css  js  c++  java
  • mysql中where子句中使用别名查询出现问题

    一、问题

      在mysql中给字段起别名后,where子句中使用别名查询出现问题

    SELECT
    s.sid AS 学号,
    s.sname AS 姓名,
    COUNT(sc.course_id) AS 选课数,
    SUM(IFNULL(sc.num,0)) AS 总成绩
    FROM
    student s
    LEFT JOIN
    score sc
    ON
    '学号'=sc.student_id
    GROUP BY
    '学号';

    二、解决方案

      别名分为两类:

    1、表的别名,比如FROM student s,s就是student的别名;
    2、查询结果中,对字段起别名,比如s.id as '学号',学号就是s.id的别名;
    第二类别名,是建立在第一类别名的基础上的。而where中,只能使用表的别名+属性名(比如where s.id=1),而不能使用‘学号=1’。

    SELECT ancestors,CONCAT(',',ancestors,',') AS 'wmtest' FROM `sys_company`  WHERE CONCAT(',',ancestors,',') LIKE '%,28,%';

    这里where 后面不能写成  where 'wmtest' like '%,28,%';这样就报错了,不能识别'wmtest'这一列。

  • 相关阅读:
    tkinter_战队数据查询系统
    python_tkinter组件
    python_tkinter基本属性
    python_tkinter组件摆放方式
    python_推导式
    python_装饰器
    python_模块1
    python_生成随机验证码
    linux基础_使用指令3
    linux部署django项目流程(全)
  • 原文地址:https://www.cnblogs.com/yoyoma0355/p/15138152.html
Copyright © 2011-2022 走看看