Jump to content

Limit A Particular Column


dweb

Recommended Posts

Hi

 

I have the following code

 

$result = mysql_query("SELECT * FROM products");
while($row = mysql_fetch_array($result))
{
echo $row['name'];
}

 

and the following DB table

 

id,type,name

1,1,Mouse

2,1,Printer

3,1,Keyboard

4,2,Laptop

5,2,Desktop

6,2,Tablet

7,2,Mp3

 

As you can see, the columns are `ID`, `Type` and `Name` and at the moment it comes back like

 

Mouse

Printer

Keyboard

Laptop

Desktop

Tablet

Mp3

 

My database also has lots of other products in it, that is just a snippet

 

At the moment it returns all the products, but what I want to do is limit it to that it only returns 2 rows from each type (2nd column), so it would look like

 

Mouse

Printer

Laptop

Desktop

 

Can anyone help? I've tried LIMIT but cannot see how to do this

 

thanks

Edited by dweb
Link to comment
Share on other sites

Don't ask me why or how it works... ;)

$sql = "SELECT type, name
FROM products
WHERE (
SELECT COUNT(id) FROM products AS p
WHERE p.type = products.type AND p.id < products.id
) < 2"; //change the last number (currently 2) to the number of desired results per group.

Link to comment
Share on other sites

Mine required a minor amendment - ORDER BY TYPE added

 

SELECT id, name
FROM (
       SELECT id, name,
       @row := IF(type=@prev, @row+1, 1) as row,
       @prev := type as type
       FROM products
       ORDER BY type
       ) x
       JOIN (SELECT @row := 0, @prev:=0) as y
WHERE row < 3;

 

On a table with 182,000+ records it took 0.2 seconds.

JCBones version is still running.

Link to comment
Share on other sites

Mine required a minor amendment - ORDER BY TYPE added

 

SELECT id, name
FROM (
SELECT id, name,
@row := IF(type=@prev, @row+1, 1) as row,
@prev := type as type
FROM products
ORDER BY type
) x
JOIN (SELECT @row := 0, @prev:=0) as y
WHERE row < 3;

 

On a table with 182,000+ records it took 0.2 seconds.

JCBones version is still running.

for some reason it returns every record, do you know why that might be?
Link to comment
Share on other sites

Barands version is a much better way, as it uses only one pass.

 

Try this one: Make sure you put an index on 'type'.

SELECT id, name, @num := if( @type =
type , @num +1, 1 ) AS row_number, @type :=
type AS dummy
FROM products
FORCE INDEX (
type )
GROUP BY type, name
HAVING row_number <=2

 

I do believe this is a one pass solution, so it should be the fastest. Hoping Barand will approve.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.