什么是正确的sql语法?

时间:2019-12-09 14:57:46

标签: mysql

SELECT employee.employ_id, employee.first_name, employee.last_name, works_with.total_sales
FROM employee
JOIN works_with
IN employee.employ_id = (
    SELECT works_with.employ_id
    FROM works_with
    WHERE works_with.total_sales > '100000'
);

我需要找到员工的ID,名字,姓氏,总销售额> 100000的总销售额,这是我的mysql代码。

它表明从“ IN employee.employ_id ....”,它没有编译并没有找到mariaDB服务器版本的手册。

然后,什么是正确的mysqlcode ??

1 个答案:

答案 0 :(得分:1)

您在查询中犯了语法错误。您正在使用IN,并且应该使用ON。查询的工作方式不太正确,因此您需要将其调整为类似以下内容:

SELECT  e.employ_id,
        e.first_name,
        e.last_name,
        ww.total_sales
  FROM  employee e
    JOIN works_with ww ON ww.employ_id = e.employ_id
  WHERE e.employ_id IN (
                       SELECT  employ_id
                         FROM  works_with
                         WHERE works_with.total_sales > '100000'
                       );

但是您现在可能希望重写查询,因为它现在有点混乱(imo):

SELECT e.employ_id,
       e.first_name,
       e.last_name,
       ww.total_sales
  FROM employee e
    JOIN works_with ww ON e.employ_id = ww.employ_id
  WHERE ww.total_sales > '100000';

根据OP注释对每个员工多行进行编辑

您将收到多行,因为JOIN每位员工产生多行。要获取SUM中的总数total_sales,请使用以下修改的查询:

SELECT  e.employ_id,
        e.first_name,
        e.last_name,
        SUM(ww.total_sales) AS total_sales
  FROM  employee e
    JOIN works_with ww ON ww.employ_id = e.employ_id
  WHERE e.employ_id IN (
                       SELECT  employ_id
                         FROM  works_with
                         WHERE works_with.total_sales > '100000'
                       )
  GROUP BY e.employ_id, e.first_name, e.last_name;