[英]How to get number of records along with min/max values in MySQL?
I have a search form (running MySQL 5.0.88
) to drill down through a product table. 我有一个搜索表单(运行MySQL 5.0.88
)以向下钻取产品表。 The search is a 2-step search. 搜索是两步搜索。 First run gets the number of results
, 2nd run gets the data of records xy
. 第一次运行获取number of results
,第二次运行获取data of records xy
的data of records xy
。
I'm displaying min/max prices across all records by default and now want to update the min/max values based on the query results. 我默认显示所有记录的最低/最高价格,现在要根据查询结果更新最低/最高价格。 I was thinking the best way to do this is inside the inital query, that gets the number of records. 我当时想最好的方法是在初始查询中,它获取记录数。
Currently it looks like this: 当前看起来像这样:
SELECT COUNT( a.id ) AS recordcount
, a.nos
, a.nos_anzeige
FROM artikelstammdaten a
WHERE
a.iln != "1111111111111" AND a.iln != "2222222222222" AND a.iln != "7777777777777"
AND a.aktiv = "ja"
AND a.artikelnummer LIKE '%name%'
AND (a.modus = "OPEN"
OR a.iln IN ( 55555555555,66666666666,2222222222222 )
)
GROUP BY a.iln, a.artikelnummer, a.preis_aktuell, a.artikelbezeichnung
HAVING (( sum(a.bestand) != 0 ) OR (a.nos = "ja" AND a.anzeige = "ja" ))
Which gives me all matching records. 这给了我所有匹配的记录。
Problem : 问题 :
Actually I only want the number of records (~ query.recordcount
) in a single record and not the individual records one by one (my attempt with COUNT
doesn't work). 实际上,我只希望单个记录中的记录数( query.recordcount
),而不是单个记录一个一个的记录(我尝试使用COUNT
无效)。 Something like this: 像这样:
totalrecords min-price max-price
12345 9.99 1.204
where I can try to select min/max values across the resultset. 在这里我可以尝试在结果集中选择最小值/最大值。
Question : 问题 :
How do I have to modify my query, so I'm only getting the totalrecords (and can try to add min/max values)? 我该如何修改我的查询,以便仅获得总记录(并且可以尝试添加最小值/最大值)?
THANKS! 谢谢!
** EDIT: ** **编辑:**
My table looks like this: 我的桌子看起来像这样:
CREATE TABLE dummy (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`iln` VARCHAR(13) NULL DEFAULT NULL,
`ean` VARCHAR(35) NULL DEFAULT NULL,
`artikelnummer` VARCHAR(35) NULL DEFAULT NULL,
`preis_ek` DECIMAL(12,2) NULL DEFAULT NULL,
`preis_vk` DECIMAL(12,2) NULL DEFAULT NULL,
`firma` VARCHAR(35) NULL DEFAULT NULL,
`nos` VARCHAR(4) NULL DEFAULT NULL,
`nos_anzeige` VARCHAR(4) NULL DEFAULT NULL,
`aktiv` VARCHAR(4) NULL DEFAULT NULL,
`modus` VARCHAR(4) NULL DEFAULT NULL,
`bestand` DECIMAL(10,0) NULL DEFAULT '0'
)
Prices would be preis_ek
and preis_vk
where I'm looking for min/max for each. 价格将是preis_ek
和preis_vk
,在这里我要为它们查找最小/最大。 THANKS! 谢谢!
EDIT : Running the query like it is gives me a resultset like this: 编辑 :像这样运行查询给我这样的结果集:
"id" "nos" "nos_anzeige" "MIN(a.preis_ek)"
"1153837" "nein" "nein" "25,10"
"1153797" "nein" "nein" "12,40"
....
which picks the min
per record not across the resultset, if I'm not mistaken 如果我没记错的话,它会选择不跨结果集的每条记录的min
You can use 您可以使用
SELECT count(recordcount),
Max(max_price),
Min(min_price)
FROM (SELECT Count(a.id) AS recordcount,
a.nos,
a.nos_anzeige,
Max(preis_ek) AS max_price,
Min(preis_ek) AS min_price
FROM artikelstammdaten a
WHERE a.iln != "1111111111111"
AND a.iln != "2222222222222"
AND a.iln != "7777777777777"
AND a.aktiv = "ja"
AND a.artikelnummer LIKE '%name%'
AND ( a.modus = "open"
OR a.iln IN ( 55555555555, 66666666666, 2222222222222 ) )
GROUP BY a.iln,
a.artikelnummer,
a.preis_aktuell,
a.artikelbezeichnung
HAVING ( ( Sum(a.bestand) != 0 )
OR ( a.nos = "ja"
AND a.anzeige = "ja" ) )) temp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.