Php 查询畅销书格式无法正常工作

Php 查询畅销书格式无法正常工作,php,mysql,Php,Mysql,我试图问我的数据库一个问题:“什么样的股票形式最畅销?”。下面是一些示例数据和组成数据库本身的代码。我使用phpMyadmin来创建数据库,因此这是来自Myadmin的数据转储 CREATE TABLE IF NOT EXISTS `tbl_sale` ( `id_sale` int(11) NOT NULL AUTO_INCREMENT, `id_stock` int(11) NOT NULL, `id_customer` int(11) NOT NULL, `id_employee` int

我试图问我的数据库一个问题:“什么样的股票形式最畅销?”。下面是一些示例数据和组成数据库本身的代码。我使用phpMyadmin来创建数据库,因此这是来自Myadmin的数据转储

CREATE TABLE IF NOT EXISTS `tbl_sale` (
`id_sale` int(11) NOT NULL AUTO_INCREMENT,
`id_stock` int(11) NOT NULL,
`id_customer` int(11) NOT NULL,
`id_employee` int(11) NOT NULL,
`id_invoice` int(11) NOT NULL,
PRIMARY KEY (`id_sale`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=32 ;

INSERT INTO `tbl_sale` (`id_sale`, `id_stock`,`id_customer`,`id_employee`, `id_invoice`) VALUES
(2, 2, 1, 1, 1),
(3, 1, 1, 1, 1),
(4, 3, 1, 1, 1),
(5, 4, 2, 2, 2),
(6, 5, 2, 2, 2),
(7, 6, 3, 3, 3),
(8, 7, 4, 4, 4),
(9, 8, 4, 5, 4),
(10, 9, 4, 5, 4),
(11, 10, 4, 6, 4),
(12, 11, 5, 7, 5),
(13, 12, 5, 7, 5),
(14, 13, 6, 8, 6),
(15, 14, 6, 8, 6),
(16, 15, 6, 8, 6),
(17, 16, 7, 9, 7),
(18, 17, 8, 10, 8),
(19, 18, 8, 10, 8),
(20, 19, 9, 1, 9),
(21, 20, 9, 1, 9),
(22, 21, 9, 1, 9),
(23, 22, 9, 1, 9),
(24, 1, 10, 2, 10),
(25, 2, 11, 3, 11),
(26, 3, 12, 4, 12),
(27, 4, 12, 4, 12),
(28, 5, 13, 5, 13),
(29, 6, 14, 6, 14),
(30, 7, 15, 7, 15),
(31, 8, 16, 7, 16);

 CREATE TABLE IF NOT EXISTS `tbl_stock` (
`id_stock` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(25) NOT NULL,
`quantity` int(11) NOT NULL,
`price_cost` decimal(10,2) NOT NULL,
`price_sale` decimal(10,2) NOT NULL,
`weight` int(25) NOT NULL,
`id_format` int(11) NOT NULL,
`id_genre` int(11) NOT NULL,
PRIMARY KEY (`id_stock`)
 ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=23 ;

INSERT INTO `tbl_stock` (`id_stock`, `name`, `quantity`, `price_cost`,`price_sale`, `weight`, `id_format`, `id_genre`) VALUES
(1, 'Star Wars: The Force Awak', 10, 2.39, 10.00, 16, 1, 1),
(2, 'The Hunger Games: Mocking', 15, 2.28, 11.00, 16, 3, 2),
(3, 'Deadpool', 20, 4.00, 15.99, 16, 1, 14),
(4, 'The Jungle Book', 10, 1.78, 13.99, 16, 1, 1),
(5, 'Captain America: Civil Wa', 13, 2.59, 12.99, 16, 3, 1),
(6, 'Batman v Superman: Dawn o', 15, 2.99, 14.99, 16, 3, 14),
(7, 'The Huntsman: Winter''s Wa', 14, 2.99, 13.00, 16, 1, 5),
(8, 'The Magnificent Seven', 10, 1.36, 11.99, 16, 1, 16),
(9, 'Hardcore Henry', 5, 1.24, 10.99, 16, 3, 18),
(10, 'Doctor Strange', 8, 2.54, 14.99, 16, 1, 2),
(11, 'Power Rangers ', 9, 0.97, 13.99, 16, 3, 14),
(12, '10 Cloverfield Lane', 5, 1.34, 12.99, 16, 3, 10),
(13, 'Star Wars Battlefront', 10, 8.49, 29.50, 100, 6, 18),
(14, 'Quantum Break', 20, 15.78, 38.61, 100, 6, 18),
(15, 'Call of Duty: Black Ops I', 12, 9.89, 30.00, 100, 5, 18),
(16, 'One Voice', 5, 1.52, 9.99, 58, 2, 21),
(17, 'NOW That''s What I Call Mu', 3, 1.29, 13.00, 55, 2, 21),
(18, 'Ratchet and Clank', 10, 7.89, 28.00, 100, 6, 17),
(19, 'Uncharted 4: A Theif''s En', 10, 7.89, 44.00, 100, 6, 18),
(20, 'Dark Sould III', 10, 9.99, 38.99, 100, 5, 19),
(21, 'Grand Theft Auto V', 12, 11.99, 37.49, 100, 5, 20),
(22, 'FIFA 16', 7, 5.89, 34.95, 100, 6, 21);

CREATE TABLE IF NOT EXISTS `tbl_stock_format` (
`id_format` int(11) NOT NULL AUTO_INCREMENT,
`format` varchar(25) NOT NULL,
  PRIMARY KEY (`id_format`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=8 ;

INSERT INTO `tbl_stock_format` (`id_format`, `format`) VALUES
(1, 'DVD'),
(2, 'CD'),
(3, 'Blu-Ray'),
(4, 'Vinyl'),
(5, 'Playstation 4'),
(6, 'Xbox One');
我尝试使用的查询是:

SELECT id_format, MAX(MAX_COUNT)
FROM(
SELECT id_format, COUNT(id_format) AS MAX_COUNT
SELECT*FROM tbl_sale, tbl_stock_format
WHERE tbl_sale.id_format = tbl_stock_format.id_format
GROUP BY id_format) AS COUNT
但是,您会遇到以下错误:

1064-您的SQL语法有错误;检查与您的MySQL服务器版本对应的手册,以了解在第4行的“SELECT*FROM tbl_sale,tbl_stock_format,其中tbl_sale.id_format=tbl_stock_form”附近使用的正确语法


有谁能告诉我我做错了什么吗?

这个问题完全是胡说八道。您选择的表列不属于该表:
tbl\u sale.id\u format
,您有多个无结构的select查询

尝试此查询。如果这没有输出正确的结果,那么这是一个比您所展示的更好的开始

SELECT tbl_stock_format.id_format, tbl_stock_format.format, COUNT(tbl_stock_format.id_format) AS count
FROM tbl_stock_format 
INNER JOIN tbl_stock
ON tbl_stock.id_format = tbl_stock_format.id_format
INNER JOIN tbl_sale
ON tbl_sale.id_stock = tbl_stock.id_stock
GROUP BY tbl_stock_format.id_format
ORDER BY count DESC

这个问题完全是胡说八道。您选择的表列不属于该表:
tbl\u sale.id\u format
,您有多个无结构的select查询

尝试此查询。如果这没有输出正确的结果,那么这是一个比您所展示的更好的开始

SELECT tbl_stock_format.id_format, tbl_stock_format.format, COUNT(tbl_stock_format.id_format) AS count
FROM tbl_stock_format 
INNER JOIN tbl_stock
ON tbl_stock.id_format = tbl_stock_format.id_format
INNER JOIN tbl_sale
ON tbl_sale.id_stock = tbl_stock.id_stock
GROUP BY tbl_stock_format.id_format
ORDER BY count DESC

我猜您需要嵌套第二个SELECT语句。
作为MAX_COUNT SELECT*FROM
肯定是错误的。你想做什么?@user1032531我想知道什么样的股票格式最畅销,所以我想知道我有哪些不同格式的股票销量最高。我猜你需要嵌套第二个SELECT语句。
作为MAX_COUNT SELECT*FROM
肯定是错误的。你想做什么?@user1032531我想知道什么样的股票格式最畅销,所以我想知道我有哪些不同格式的股票销量最高。这很好,谢谢你,马特,当你阅读你写的东西时,这很有意义!这真是太好了,谢谢你,马特,当你读到你写的东西时,这很有意义!