[英]mysql group_by only returns first row
I'm new here and and need some help with mysql query. 我是新来的,并且需要有关mysql查询的一些帮助。
I have this query to retrieve several rows in a join query. 我有此查询来检索联接查询中的几行。
SELECT category.category, category_color.color,
SUM(sales.quantity) AS quantity, SUM(sales.price) AS total, sales.id_payment_type,
sales.price
FROM sales
INNER JOIN
category USING (id_category)
INNER JOIN
shop_session USING (id_shop_session)
INNER JOIN
category_color USING (id_category)
WHERE shop_session.is_active = 1
AND category.id_lang = "._ID_LANG_."
AND shop_session.id_shop = ".$_SESSION['id_shop']."
GROUP BY category.category
ORDER BY category.category
I've achieved grouping by category is the first purpose , but when I need to retrive some data like "price" or "id_payment_type" mysql only returns the first row, and I need to get all rows from the query. 我已经实现了按类别分组的首要目的,但是当我需要检索诸如“ price”或“ id_payment_type”之类的数据时,mysql只返回第一行,而我需要从查询中获取所有行。 I don't know how can I solve this query.
我不知道如何解决此查询。 Please help!
请帮忙!
My sales db structure: 我的销售数据库结构:
CREATE TABLE IF NOT EXISTS `sales` (
id_sale
int(11) NOT NULL AUTO_INCREMENT, id_product
int(11) unsigned NOT NULL, id_category
int(11) unsigned NOT NULL, id_employee
int(11) unsigned NOT NULL, id_payment_type
int(11) unsigned NOT NULL, id_sale_type
int(11) unsigned NOT NULL, id_shop_session
int(11) unsigned NOT NULL, position
int(11) NOT NULL, quantity
int(11) NOT NULL, price
decimal(17,2) NOT NULL, amount
float NOT NULL, invoice
int(1) NOT NULL, note
varchar(150) COLLATE utf8_unicode_ci NOT NULL, date_sale
int(11) DEFAULT NULL, PRIMARY KEY ( id_sale
, id_product
, id_category
, id_employee
, id_payment_type
, id_sale_type
, id_shop_session
), KEY id_sale
( id_sale
) ) id_sale
int(11)非空AUTO_INCREMENT, id_product
int(11)无符号非空, id_category
int(11)无符号非空, id_employee
int(11)无符号非空, id_payment_type
int(11)无符号非空, id_sale_type
int(11) unsigned NOT NULL, id_shop_session
int(11)unsigned NOT NULL, position
int(11)NOT NULL, quantity
int(11)NOT NULL, price
小数(17,2)NOT NULL, amount
浮动NOT NULL, invoice
int(1)NOT NULL, note
VARCHAR(150)COLLATE utf8_unicode_ci NOT NULL, date_sale
INT(11)DEFAULT NULL,PRIMARY KEY( id_sale
, id_product
, id_category
, id_employee
, id_payment_type
, id_sale_type
, id_shop_session
),KEY id_sale
( id_sale
))
And this is my db structure for category table: 这是我的类别表的数据库结构:
CREATE TABLE IF NOT EXISTS `category` (
id_category
int(10) unsigned NOT NULL, id_lang
int(10) unsigned NOT NULL, category
varchar(30) COLLATE utf8_unicode_ci NOT NULL, is_active
int(1) NOT NULL ) id_category
int(10)无符号NOT NULL, id_lang
int(10)无符号NOT NULL, category
varchar(30)COLLATE utf8_unicode_ci NOT NULL, is_active
int(1)NOT NULL)
SELECT category.category, category_color.color,
SUM(sales.quantity) AS quantity, SUM(sales.price) AS total, sales.id_payment_type,
sales.price FROM sales
INNER JOIN category USING (id_category)
INNER JOIN shop_session USING (id_shop_session)
INNER JOIN category_color USING (id_category)
WHERE shop_session.is_active = 1
AND category.id_lang = "._ID_LANG_."
AND shop_session.id_shop = ".$_SESSION['id_shop']."
GROUP BY category.category
ORDER BY category.category
This query will give you the first row of every category you have because you are using a group by and i say the first because you are using an order by with an implicit ASC. 此查询将为您提供每个类别的第一行,因为您使用的是分组依据,而我说的第一行是因为您使用的是带有隐式ASC的订单。
If you want all the id_payment_type or prices of every category you can't group by. 如果您想要所有类别的所有id_payment_type或价格,则无法分组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.