Jump to content

Recommended Posts

Been a while since iv been on here... Any help appreciated:

 

What I am trying to achieve is to select a specific row from a table, then get the rest of the rows. I have found the solution to this using:

 

GROUP BY (id = 1);

 

However I still want to filter the rest of the rows with the ORDER BY clause.

 

When I try to do this, it does not return the GROUP BY id first.

 

cheers.

Link to comment
https://forums.phpfreaks.com/topic/256813-group-by-before-order-by-desc/
Share on other sites

The reason I am using GROUP BY is because I still want to return all other rows, but select one specific row to display first.

 

the table design is (simplified):

 

create table person (

  id int unsigned not null AUTO_INCREMENT

  name varchar(40) not null,

  PRIMARY KEY (id)

);

 

create table order (

  id int unsigned not null AUTO_INCREMENT,

  personid int unsigned not null,

  productid int unsigned not null,

  name varchar(40) not null,

  PRIMARY KEY (id),

  FOREIGN KEY (personid) references person(id),

  FOREIGN KEY (productid) references product(id)

);

 

create table product (

  id int unsigned not null,

  name varchar(40) not null,

  PRIMARY KEY (id)

);

 

 

query:

 

select a.*, b.*, c.*

FROM person AS a

LEFT JOIN order AS b ON a.id = b.personid

LEFT JOIN product AS c ON c.id = b.productid

WHERE

c.name = 'some product'

GROUP BY (b.id = 43) DESC

ORDER BY b.name;

 

Please note the tables and query have been condensed a lot, just to give the idea of what im trying to achieve.

 

So I am basically wanting to get the data for orders that have a certain product name, in name order, but I also want to select a specific order id, even if that has the product name in it or not. I want that row to be the first returned row.

 

I appreciate your guidence.

 

 

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.