473,408 Members | 2,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Limiting

sks
Hi,

I have a products table which apart from other things has a column called
manufacturer. I want to run a query that returns x number of products PER
manufacturer for each manufacturer that matches some criteria.
table::products
--------
id
name
cost
stock
manufacturer
I can only figure out how to limit it to 1, eg

"select manufacturer, name from products where manufacturer like 'A%' group
by manufacturer order by manufacturer, name"

which will return something like this

ManufacturerX, ProductA
ManufacturerY, ProductB
ManufacturerZ, ProductC

What I'd like to do is have something like this returned:

ManufacturerX, ProductA
ManufacturerX, ProductB
ManufacturerX, ProductC
ManufacturerY, ProductD
ManufacturerY, ProductE
ManufacturerY, ProductF
ManufacturerZ, ProductG
ManufacturerZ, ProductH
ManufacturerZ, ProductI

Depending on what limit I set per manufacturer.

Hope that makes some sense !
Jul 20 '05 #1
1 1567
sks wrote:
I want to run a query that returns x number of products PER
manufacturer for each manufacturer that matches some criteria.


I thought about this for a few minutes, and no "pretty" single-query
solution comes to mind.

You can always do it in multiple queries:
1. Fetch list of distinct manufacturer names
2. Loop over list of manufacturers
3. SELECT manufacturer, name FROM products
WHERE manufacturer = ? ORDER BY name LIMIT 3
4. Concatenate the results of all these queries together in your
application, and then output.

Another method would be to do build up a UNION query, if you are using
MySQL 4.0.x or later.

SELECT manufacturer, name FROM products
WHERE manufacturer = "ManufacturerX" ORDER BY name LIMIT 3
UNION
SELECT manufacturer, name FROM products
WHERE manufacturer = "ManufacturerY" ORDER BY name LIMIT 3
UNION
SELECT manufacturer, name FROM products
WHERE manufacturer = "ManufacturerZ" ORDER BY name LIMIT 3

Regards,
Bill K.
Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: nzanella | last post by:
Hello, I am running SQL Server 2000. I would like to know whether Microsoft Transact-SQL has a method for limiting the result set from a query in a way analogous to MySQL's LIMIT keyword, so...
2
by: murray_shane56 | last post by:
We currently have a routine that "forks" out (to use the unix term)TSQL commands to run asynchronously via SQL Agent jobs. Each TSQL command gets its own Job, and the job starts immediately after...
1
by: Daniel | last post by:
limiting access to files with asp.net is there any way i can make a file only accessible to certain users of my website? my files are to large to copy to a temp directory and they are of many...
2
by: Jo Davis | last post by:
access fe and access be. later the be might be sql server I don't want people to pass this application around. And I want control over usage. I want it to 'expire' after a while. I have fairly...
2
by: Sara | last post by:
I have followed instructions on the http://allenbrowne.com/tips.html for limiting a report to a date range. At the bottom there is a note that says You will end up using this form for all sorts...
5
by: randyelliott | last post by:
Good Day, I have a MS Access (Access 2000 now upgraded to 2003) database that tracks customer information. One function of this database is to create an encrypted license file for our software,...
2
by: Gerry Abbott | last post by:
Hi all, Im using an ubound form and recordsets for data update. Id like to trap invalid entries on this form. How can i limit the number of characters the user enters for a particular field. ? ...
4
by: N J | last post by:
Hi, I ahve developed a program using access and am distributing it using MDE's, I ahve had many requests for a demo. I was thinking of limiting the number of records to say 100? If anyone has...
3
by: Peter Silva | last post by:
Hi folks, I have a need in a network data distribution application to send out data to folks who want it using the protocol of their choice. IŽd like it to support a variety of protocols and I...
4
WyvsEyeView
by: WyvsEyeView | last post by:
I have a datasheet form in which one field is a combo box that will potentially contain hundreds of records. I've read about several methods of speeding up such combo boxes or limiting their initial...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.