未知列“building_requirements_count”在“字段列表”中
我用 php 和 mysql 做了一个小游戏。现在我创建的 SQL 查询之一出现问题。这个想法是查询检查用户是否有足够的材料。
我有一个查询,如果我像这样使用它,它会起作用:
SELECT
(
SELECT COUNT(*)
FROM building_requirements
WHERE building_id = '1'
) as building_requirements_count,
(
SELECT COUNT(*)
FROM user_materials, building_requirements
WHERE user_materials.material_id = building_requirements.material_id
AND user_id = '27'
AND building_id = '1'
AND (user_material_amount >= building_material_amount) = 1
) as user_materials_count;
但是当我添加一列使用这些子查询的结果时,它会失败:
SELECT
(
SELECT COUNT(*)
FROM building_requirements
WHERE building_id = '1'
) as building_requirements_count,
(
SELECT COUNT(*)
FROM user_materials, building_requirements
WHERE user_materials.material_id = building_requirements.material_id
AND user_id = '27'
AND building_id = '1'
AND (user_material_amount >= building_material_amount) = 1
) as user_materials_count,
building_requirements_count = user_materials_count as enough_materials;
我收到错误:
#1054 - Unknown column 'building_requirements_count' in 'field list'
有人可以向我解释为什么我不能使用子查询在这里?我该如何解决这个问题?
I'm make a little game in php with mysql. Now I have a problem with one of the sql query's I created. The idea is that the query checks if the user has enough materials.
I have a query that if I use it like this it works:
SELECT
(
SELECT COUNT(*)
FROM building_requirements
WHERE building_id = '1'
) as building_requirements_count,
(
SELECT COUNT(*)
FROM user_materials, building_requirements
WHERE user_materials.material_id = building_requirements.material_id
AND user_id = '27'
AND building_id = '1'
AND (user_material_amount >= building_material_amount) = 1
) as user_materials_count;
But when I add one column that use the result of those subquery's it fails:
SELECT
(
SELECT COUNT(*)
FROM building_requirements
WHERE building_id = '1'
) as building_requirements_count,
(
SELECT COUNT(*)
FROM user_materials, building_requirements
WHERE user_materials.material_id = building_requirements.material_id
AND user_id = '27'
AND building_id = '1'
AND (user_material_amount >= building_material_amount) = 1
) as user_materials_count,
building_requirements_count = user_materials_count as enough_materials;
I get the error:
#1054 - Unknown column 'building_requirements_count' in 'field list'
Can someone explain to me why I can't use the results of the subquery here? And how I can fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想你的
意思可能是......
Where you have
I think you may mean...
因为您的表定义中没有名为“building_requirements_count”的字段。
除了WHERE部分之外,这里不允许使用自定义字段。
为什么不在查询的 WHERE 部分使用自定义字段?
编辑:
您可以更轻松地从数据库中获取每个值并在 PHP 中进行计算。
Cause the is no field called "building_requirements_count" in your table definition.
You are not allowed to use self-defined fields here except for the WHERE part.
Why don't you use your self-defined fields in the WHERE section of your query?
EDIT:
It would be easier for you to get each value seperate out of the DB and do the calculating stuff in PHP.