我的SQL出了什么问题? (MySQL语法错误,代码1064)

时间:2018-01-16 17:50:12

标签: mysql sql join

我有

select created_at, sales_flat_order.customer_id,
    customer_firstname, customer_lastname, customer_email, 
    sales_flat_order_address.company,
    SUM(grand_total)
from sales_flat_order
left join sales_flat_order_address 
    on sales_flat_order.entity_id = sales_flat_order_address.entity_id
    where created_at >= '2016-06-01 05:00:00' /* 5h difference */
        and store_id = 1
        and `status` = 'Complete'/*
left join customer_entity
    on sales_flat_order.customer_id = customer_entity.entity_id*/
group by customer_email;

运行良好。注释内容会产生SQL语法错误,代码为1064.问题是什么?

现在同样的问题:

select customer.entity_id, customer.group_id, 
    customer.created_at,
    sfo.customer_firstname, sfo.customer_lastname,
    sfo.customer_email,
    sfo.created_at last_order,
    count(sfo.customer_id) num_orders,
    group_concat(sfo.grand_total separator '|') grand_totals
from customer_entity customer
inner join sales_flat_order sfo
    on customer.entity_id = sfo.customer_id
    where sfo.created_at >= '2016-06-01 05:00:00'
        and sfo.store_id = 1
        and sfo.`status` = 'Complete'
left outer join customer_group
    on customer.group_id = customer.customer_group_id
    where customer_group.customer_group_id
group by customer.entity_id;

谢谢大家,这一切都有充分的意义,绝对要深入了解它;这个怎么样?它有效,但如果有什么东西仍然存在,就把它撕掉。

select customer.entity_id, customer.group_id, `group`.customer_group_code,
    customer.created_at,
    `order`.customer_firstname, `order`.customer_lastname,
    `order`.customer_email, address.company,
    `order`.created_at last_order,
    count(`order`.customer_id) num_orders,
    group_concat(`order`.grand_total separator '|') grand_totals

from customer_entity customer

inner join sales_flat_order `order`
    on customer.entity_id = `order`.customer_id
left outer join customer_group `group` 
    on customer.group_id = `group`.customer_group_id
left outer join sales_flat_order_address address
    on `order`.entity_id = address.entity_id

where `order`.created_at >= '2016-06-01 05:00:00'
    and `order`.store_id = 1
    and `order`.`status` = 'Complete'

group by customer.entity_id;

我知道每个人都说要添加group by条款,但我没有其他任何想要分组的内容......?

2 个答案:

答案 0 :(得分:0)

where子句必须在所有连接之后。还有其他问题,但这是真正打破问题的问题。

如果是我,我还修复了group by子句以匹配选择列表,但MySql允许您以这种方式编写非标准SQL。添加附加表时,您可能还会出现命名冲突,因为您尚未对列名进行限定。别名会使这更容易做到最后,您的查询逻辑可能希望where子句中的某些条件表达式成为第一个ON子句的一部分。

答案 1 :(得分:0)

如果正确格式化代码,很容易理解为什么代码不起作用。导致错误的注释LEFT JOIN位于WHERE子句之后。也写适当的SQL。别名您的表名称,因为长列名称很烦人。将所有非聚合列放在GROUP BY子句中。除了MySQL之外,您的代码甚至不会在任何其他dbms中运行。

select created_at, sales_flat_order.customer_id
    , customer_firstname, customer_lastname
    , customer_email
    , sales_flat_order_address.company
    , SUM(grand_total)
from sales_flat_order
left join sales_flat_order_address on sales_flat_order.entity_id = sales_flat_order_address.entity_id
where created_at >= '2016-06-01 05:00:00' /* 5h difference */
    and store_id = 1
    and `status` = 'Complete'/*
left join customer_entity on sales_flat_order.customer_id = customer_entity.entity_id*/
group by customer_email;
相关问题