Jump to content

Group By Before Order By DESC


fry2010

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.

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.