mysql - 我的 mysql 查询中出现限制条件错误

标签 mysql sql phpmyadmin mysql-workbench

我有3张 table 客户、时间和销售额 我想查出所有客户的年收入条件是没有 child 的客户且收入必须大于我们设定的限制 我的表结构 客户

CREATE TABLE `customers` (
  `customer_id` int(11) DEFAULT NULL,
  `account_num` double DEFAULT NULL,
  `lname` varchar(50) DEFAULT NULL,
  `fname` varchar(50) DEFAULT NULL,
  `mi` varchar(50) DEFAULT NULL,
  `address1` varchar(50) DEFAULT NULL,
  `address2` varchar(50) DEFAULT NULL,
  `address3` varchar(50) DEFAULT NULL,
  `address4` varchar(50) DEFAULT NULL,
  `postal_code` varchar(50) DEFAULT NULL,
  `region_id` int(11) DEFAULT NULL,
  `phone1` varchar(50) DEFAULT NULL,
  `phone2` varchar(50) DEFAULT NULL,
  `birthdate` datetime DEFAULT NULL,
  `marital_status` varchar(50) DEFAULT NULL,
  `yearly_income` varchar(50) DEFAULT NULL,
  `gender` varchar(50) DEFAULT NULL,
  `total_children` smallint(6) DEFAULT NULL,
  `num_children_at_home` smallint(6) DEFAULT NULL,
  `education` varchar(50) DEFAULT NULL,
  `member_card` varchar(50) DEFAULT NULL,
  `occupation` varchar(50) DEFAULT NULL,
  `houseowner` varchar(50) DEFAULT NULL,
  `num_cars_owned` smallint(6) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

销售

CREATE TABLE `sales` (
  `product_id` int(11) DEFAULT NULL,
  `time_id` int(11) DEFAULT NULL,
  `customer_id` int(11) DEFAULT NULL,
  `store_id` int(11) DEFAULT NULL,
  `store_sales` float DEFAULT NULL,
  `store_cost` float DEFAULT NULL,
  `unit_sales` double DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `times` (
  `time_id` int(11) DEFAULT NULL,
  `the_date` datetime DEFAULT NULL,
  `the_day` varchar(50) DEFAULT NULL,
  `the_month` varchar(50) DEFAULT NULL,
  `the_year` smallint(6) DEFAULT NULL,
  `day_of_month` smallint(6) DEFAULT NULL,
  `month_of_year` smallint(6) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

我的问题是:-查找没有 child 且年收入大于用户在运行查询时给出的限制的客户列表。

我的查询是

SET @limit=50;
SELECT customers.`fname`, customers.`lname` ,ROUND(SUM(sales.store_sales)) as income,times.the_year
 FROM `sales` 
 LEFT JOIN times
 ON sales.time_id=times.time_id
 LEFT JOIN customers 
ON customers.customer_id=sales.customer_id 
WHERE income>@limit AND `total_children`=0
GROUP BY sales.customer_id,times.the_year 

收到此错误:#1054 - “where 子句”中的未知列“收入”

最佳答案

您别名为收入的数量是一个聚合,因此在WHERE子句中引用它没有意义。将此 WHERE 逻辑移至 HAVING 子句:

SET @limit=50;
SELECT
    c.fname,
    c.lname,
    ROUND(SUM(s.store_sales)) AS income,
    t.the_year
FROM sales s 
LEFT JOIN times t
    ON s.time_id = t.time_id
LEFT JOIN customers c
    ON c.customer_id = s.customer_id 
WHERE
    total_children = 0
GROUP BY
    c.customer_id,
    t.the_year 
HAVING
    ROUND(SUM(s.store_sales)) > @limit;

请注意,从技术上讲,我们可以在 HAVING 子句中使用别名:

HAVING income > @limit;

但这无法移植到大多数其他数据库。另外,我在查询中引入了别名,这使得它更易于阅读。

关于mysql - 我的 mysql 查询中出现限制条件错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49380152/

相关文章:

php - session 变量在本地主机上工作,但在网络服务器上不起作用

php - 我的 SQL IN 查询有问题 我将多个员工 ID 存储在一个用逗号分隔的表中

mysql GROUP_CONCAT(查询)

php - friend 的搜索查询无法正确工作

python - 将参数值插入 MySQLdb.execute() 时出现问题

mysql - 将 SQL 结果行合并在一起

sql - 在新的数据库服务器上创建新的非对称 key 会导致旧数据失效吗?

php - 使用 php 列出 mysql 数据库

javascript - 从其他 JSP 中创建的 JS 函数接收 JSP 中的值

Mysql 在较新版本的 mySQL 上返回疯狂的 "rows"结果?