一个版本的查询中出现 Mysql 错误 1111,另一版本中出现错误 1054

发布于 2024-09-24 05:48:07 字数 1738 浏览 5 评论 0原文

我有两个表:

  • books:[isbn,book_title,publisher,...]
  • inventory:[isbn,date,num_changed]

我想返回有库存的书名。我尝试连接(查询 1)并收到 1054 错误,然后我用文字值替换引用,现在收到 1111 错误。

查询 1:

SELECT `books`.`isbn`, `books`.`book_title`, SUM( `inventory`.`numbers_changed` ) AS `num`
FROM `books`
INNER JOIN `inventory` ON `books`.`isbn` = `inventory`.`isbn`
WHERE `books`.`publisher` LIKE '%pint%'
AND `num` > '0'

查询 2:

SELECT `books`.`isbn`, `books`.`book_title`, SUM( `inventory`.`numbers_changed` )
FROM `books`
INNER JOIN `inventory` ON `books`.`isbn` = `inventory`.`isbn`
WHERE `books`.`publisher` LIKE '%print%'
AND SUM( `inventory`.`numbers_changed` ) > '0'

使用的正确查询是什么?

编辑
以下是创建表查询:

CREATE TABLE IF NOT EXISTS `books` (
  `isbn` varchar(30) CHARACTER SET ascii NOT NULL,
  `book_title` varchar(255) CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
  `date_published` varchar(10) CHARACTER SET ascii NOT NULL,
  `author` varchar(40) CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
  `translator` varchar(40) CHARACTER SET utf8 COLLATE utf8_persian_ci DEFAULT NULL,
  `publisher` varchar(50) CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
  `ganre` varchar(50) CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
  `price` int(7) unsigned NOT NULL,
  `cover_pic` int(1) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`isbn`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `inventory` (
  `isbn` varchar(30) CHARACTER SET ascii NOT NULL,
  `date` varchar(10) CHARACTER SET ascii NOT NULL,
  `numbers_changed` int(5) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

I have two tables:

  • books: [isbn, book_title, publisher, ...]
  • inventory: [isbn, date, num_changed]

I want to return book titles for those which are on stock. I tried a join (query 1) and got 1054 error, then I substituted the reference with the literal value and now I get 1111 error.

query 1:

SELECT `books`.`isbn`, `books`.`book_title`, SUM( `inventory`.`numbers_changed` ) AS `num`
FROM `books`
INNER JOIN `inventory` ON `books`.`isbn` = `inventory`.`isbn`
WHERE `books`.`publisher` LIKE '%pint%'
AND `num` > '0'

query 2:

SELECT `books`.`isbn`, `books`.`book_title`, SUM( `inventory`.`numbers_changed` )
FROM `books`
INNER JOIN `inventory` ON `books`.`isbn` = `inventory`.`isbn`
WHERE `books`.`publisher` LIKE '%print%'
AND SUM( `inventory`.`numbers_changed` ) > '0'

What's the correct query to use?

Edit
Here are the create table queries:

CREATE TABLE IF NOT EXISTS `books` (
  `isbn` varchar(30) CHARACTER SET ascii NOT NULL,
  `book_title` varchar(255) CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
  `date_published` varchar(10) CHARACTER SET ascii NOT NULL,
  `author` varchar(40) CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
  `translator` varchar(40) CHARACTER SET utf8 COLLATE utf8_persian_ci DEFAULT NULL,
  `publisher` varchar(50) CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
  `ganre` varchar(50) CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL,
  `price` int(7) unsigned NOT NULL,
  `cover_pic` int(1) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`isbn`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `inventory` (
  `isbn` varchar(30) CHARACTER SET ascii NOT NULL,
  `date` varchar(10) CHARACTER SET ascii NOT NULL,
  `numbers_changed` int(5) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

叹梦 2024-10-01 05:48:07

1054 错误与引用不存在的列有关。实际的错误消息将有助于了解导致问题的原因。

1111 错误是因为您尝试在 WHERE 子句中使用聚合函数(在本例中为 SUM):

WHERE ...
  AND SUM( `inventory`.`numbers_changed` ) > '0'  
      ^
      |__ see this?

...在子查询之外。 SQL语句是从下到上检查的,所以我希望删除WHERE子句中的SUM将显示1054错误仍然没有解决。

The 1054 error is about referencing a column that doesn't exist. The actual error message would help to know what is causing the issue.

The 1111 error is because you're trying to use aggregate function (in this case, SUM) in the WHERE clause:

WHERE ...
  AND SUM( `inventory`.`numbers_changed` ) > '0'  
      ^
      |__ see this?

...outside of a subquery. SQL statements are checked from bottom to top, so I expect that removing the SUM in the WHERE clause will show that the 1054 error is still unaddressed.

疑心病 2024-10-01 05:48:07

使用having作为第二个where参数

WHERE `books`.`publisher` LIKE '%print%' 
HAVING ( COUNT(`inventory`.`numbers_changed`)  > '0')

而不是

WHERE `books`.`publisher` LIKE '%print%'
AND SUM( `inventory`.`numbers_changed` ) > '0'

use having for second where argument

WHERE `books`.`publisher` LIKE '%print%' 
HAVING ( COUNT(`inventory`.`numbers_changed`)  > '0')

instead of

WHERE `books`.`publisher` LIKE '%print%'
AND SUM( `inventory`.`numbers_changed` ) > '0'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文