sql子查询问题
我很少说英语。
我有一个 sql 子查询错误
数据库:MySQL
表类型:MyISAM
以下我的sql查询
SELECT
(SELECT sum(`total`) FROM `staff_history` WHERE `type` = 'Giriş' AND staff_id = table_1.staff_id) AS `input`,
(SELECT sum(`total`) FROM `staff_history` WHERE `type` = 'Çıkış' AND staff_id = table_1.staff_id) AS `output`,
(`input` - `output`) AS `balance`
FROM `staff_history` AS `table_1` WHERE `staff_id` = '2';
我收到这样的错误
Error code 1054, SQL status 42S22: Unknown column 'input' in 'field list'
你能帮我解决这个问题吗?
Im little speak english.
I have an sql subquery error
Database : MySQL
Table type : MyISAM
the following my sql query
SELECT
(SELECT sum(`total`) FROM `staff_history` WHERE `type` = 'Giriş' AND staff_id = table_1.staff_id) AS `input`,
(SELECT sum(`total`) FROM `staff_history` WHERE `type` = 'Çıkış' AND staff_id = table_1.staff_id) AS `output`,
(`input` - `output`) AS `balance`
FROM `staff_history` AS `table_1` WHERE `staff_id` = '2';
I get an error like this
Error code 1054, SQL status 42S22: Unknown column 'input' in 'field list'
Can you help me about this problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法使用已有的字段名称,因为它们在此范围内不可用。
您可以复制整个表达式
查询优化器可以很好地处理这个问题,但它不太可维护,因此您也可以将整个查询放在子查询中:
You cannot use the fieldnames there already, because they are not available in this scope.
You could duplicate the whole expression
The query optimizer handles this remarkable well, but it is not very maintanable, so you could also put the entire query in a subquery:
我提供1条SQL语句,1条表扫描:
I offer 1 SQL statement, 1 table scan: