开发者

How do I get the ID of a rows which have MAX and MIN values in SQL

I am trying to make the queries my website uses more efficient.

Being a bit vague about SQL, I've not really learnt how to use nested queries, but I have just managed to get something that is pretty near what I want.

I sell guitars, I have a big database with all the products with different finish options listed individually. Items have unique IDs in the dB but are grouped by their title, for example, a Gibson Les Paul Standard is listed in my dB 7 times with 7 different finish options. Not all the finish options will necessarily have the same price, and not all finish options will necessarily be in stock.

In the search results page of my website I want to be able to show:

1) Just one record per product, ie 1 record for Gibson LP Std, which can then be sub-linked to the different finishes.

2) The actual product displayed must either be the cheapest finish option, OR, the cheapest in stock.

This is currently working on my website, but it's using N+1 queries and seems to be running dreadfully slowly, but for an example of what I mean, click here: http://www.hartnollguitars.co.uk/search.asp?subcat=Gibson-Les-Pauls (if the bloody thing works)

Part one is fine, I can just group the title in SQL, it's getting part 2 out that's the problem.

Using the following SQL query I can get the lowest price and the highest price and I have counted how many variants there are, I also have the max and min stock levels.

results.Open "SELECT * FROM 
(SELECT *, count(id) as Variants, MAX(price) as highestPrice, MIN(price) as
lowestPrice, MAX(shopstock) as highestStock, MIN(shopstock) as lowestStock FROM 
products WHERE i开发者_StackOverflow中文版tem LIKE '%"& replace([searchterm]," ","%") &"%' GROUP BY item) 
AS UnknownVar LIMIT 40", conn, 3, &H0001

What I need to be able to do is get the ID value for the rows representing the max and min stock and price values.

I basically need to be able to run if/or logic on it and I am not sure if this is possible.

So, I need to be able to say

if Item_With_Cheapest_Price is in stock, display this as the thumbnail & link
else 
display first item in price sorted list where stock >=1

I also need a fall back, if none of the finishes are in stock, display the cheapest one.

The database is MySQL using ODBC connections, I am currently scripting in Classic ASP but aim to upgrade to .NET, once I've worked out how!!! :-)


I think for the order by part you should use something like

order by case
    when stock > 0 then 0
    when stock < 0 then 1
    end ascending,
    price ascending

I didn't check the syntax but that's the idea. You can google case in order by for more info.

As for the rest of you requirements I would need the table structure to better understand...


Do you know the concept of dense_rank? If not I could explain it to you. Your purpose could be solved by following queries. Have a look at this.

SELECT id,
MIN(stock) KEEP (DENSE_RANK FIRST ORDER BY stock,price) "Lowest"
,MAX(stock) KEEP (DENSE_RANK LAST ORDER BY stock,price) "Highest"
FROM products
GROUP BY id;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜