我正在用 mysql 在 php 中做一个小游戏。现在我创建的一个 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'
有人可以向我解释为什么我不能在这里使用子查询的结果吗?我该如何解决这个问题?