一个版本的查询中出现 Mysql 错误 1111,另一版本中出现错误 1054
我有两个表:
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
1054 错误与引用不存在的列有关。实际的错误消息将有助于了解导致问题的原因。
1111 错误是因为您尝试在 WHERE 子句中使用聚合函数(在本例中为 SUM):
...在子查询之外。 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:
...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.
使用having作为第二个where参数
而不是
use having for second where argument
instead of