我一直有这个问题,因此无法找到解决方案, 我需要使用union从多个表中选择以选择ID然后按日期对结果进行排序,我已经进行了此查询(并且它曾用于较旧的mysql版本)
(SELECT MAX(id),UNIX_TIMESTAMP(date) AS date,stock_id, 'Purchase' as type FROM stock_entries WHERE type='entry' GROUP BY stock_id)
UNION
(SELECT MAX(id),UNIX_TIMESTAMP(date) AS date,stock_id, 'Sales' as type FROM stock_entries WHERE type='sales' GROUP BY stock_id)
UNION (select id,date, `order` AS stock_id, 'Inventory' as type FROM inventory_trans)
ORDER BY date ASC
我想要的是,我每次生成一个ID,还有一种类型,类型是购买或销售或库存,所有结果应按日期排序(较旧 - >更新)来自两个表,每当我运行上述查询时,我都会遇到此错误:
1140: In aggregated query without GROUP BY, expression #3 of SELECT list contains nonaggregated column 'system.inventory_trans.order'; this is incompatible with sql_mode=only_full_group_by
如何使用此SQL查询来根据日期框架(日期ASC)获取我所需要的 - 旧结果 - >更新的结果
谢谢
答案 0 :(得分:0)
请试试这个查询。
SELECT * FROM
(SELECT s.id,UNIX_TIMESTAMP(s.date) as date,s.stock_id,
M.newType AS type
FROM stock_entries s
INNER JOIN
(SELECT MAX(id) as maxid, stock_id,IF(type='entry','Purchase','Sales') as newType
FROM stock_entries
WHERE type='entry' OR type='sales'
GROUP BY stock_id,newtype) as M
ON M.maxid = s.id
)T1
UNION ALL
(select id,`date`, `order` AS stock_id, 'Inventory' as type FROM inventory_trans)
ORDER BY date ASC