我有具有这种结构的用户和订单表(简化了问题):
用户
用户ID
ownerid(用户所有者ID,如主要用户)
公司
订单
id
日期(下订单日期)
user_id
总计
我需要获取与一个所有者(例如ownerid,例如52个)相关的所有用户,这些所有者在选定时期内下订单,并带有一些字段的数组(请参阅查询)。不幸的是,我的请求给出一个(不是数组)而不是当前结果(有时甚至在所有字段中都为null)。这是我的要求:
SELECT
ei_users.userid as id,
ei_users.company as company,
ei_users.city as city,
ei_users.user_type as user_type,
ei_users.registered as registered,
count(ei_orders.id) AS orders,
sum(ei_orders.total) AS revenue
FROM
ei_orders,
ei_users
WHERE
ei_users.userid = ei_orders.user_id
&& ei_users.ownerid = 52
&& DATE_FORMAT(ei_orders.date, "%b") = "Apr"
&& DATE_FORMAT(ei_orders.date, "%Y") = "2019"
请让我知道我的请求出了什么问题以及为什么我没有得到结果数组?
我发现了我的错误,我忘记在末尾添加 GROUP BY ei_users.userid :
SELECT
ei_users.userid as id,
ei_users.company as company,
ei_users.city as city,
ei_users.user_type as user_type,
ei_users.registered as registered,
count(ei_orders.id) AS orders,
sum(ei_orders.total) AS revenue
FROM
ei_orders,
ei_users
WHERE
ei_users.userid = ei_orders.user_id
&& ei_users.ownerid = 52
&& DATE_FORMAT(ei_orders.date, "%b") = "Apr"
&& DATE_FORMAT(ei_orders.date, "%Y") = "2019"
GROUP BY ei_users.userid
答案 0 :(得分:0)
好像我忘了最后添加:
GROUP BY ei_users.userid