未知列“building_requirements_count”在“字段列表”中

发布于 2024-09-08 22:14:21 字数 1202 浏览 3 评论 0原文

我用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若能看破又如何 2024-09-15 22:14:22

我想你的

building_requirements_count = user_materials_count as enough_materials;

意思可能是......

building_requirements_count - user_materials_count as enough_materials;

Where you have

building_requirements_count = user_materials_count as enough_materials;

I think you may mean...

building_requirements_count - user_materials_count as enough_materials;
许仙没带伞 2024-09-15 22:14:22

因为您的表定义中没有名为“building_requirements_count”的字段。
除了WHERE部分之外,这里不允许使用自定义字段。

为什么不在查询的 WHERE 部分使用自定义字段?

编辑:
您可以更轻松地从数据库中获取每个值并在 PHP 中进行计算。

$result = mysql_query("SELECT COUNT(*) FROM building_requirements WHERE building_id = '1'");
if ($result) {
  $row = mysql_fetch_row($result);
  $building_requirements_count  = $row[0];
}
else {
  $building_requirements_count = 0;
}

$query = "  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";

$result2 = mysql_query($query);
if ($result2) {

  $row = mysql_fetch_row($result2);
  $user_material_count  = $row[0];
}
else {
  $user_material_count = 0;
}

$enough_materials = ( $user_material_count >= $building_requirements_count) ? true : false;

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.

$result = mysql_query("SELECT COUNT(*) FROM building_requirements WHERE building_id = '1'");
if ($result) {
  $row = mysql_fetch_row($result);
  $building_requirements_count  = $row[0];
}
else {
  $building_requirements_count = 0;
}

$query = "  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";

$result2 = mysql_query($query);
if ($result2) {

  $row = mysql_fetch_row($result2);
  $user_material_count  = $row[0];
}
else {
  $user_material_count = 0;
}

$enough_materials = ( $user_material_count >= $building_requirements_count) ? true : false;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文