Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Troubleshooting
Research
Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

sql order by - asc and desc in same query

Avatar of phillystyle123
phillystyle123Flag for United States of America asked on
PHP
6 Comments1 Solution573 ViewsLast Modified:
I want to list a record 1st if it's "order_id" is 1, (then 2, 3, etc.). If an "order_id" isn't specified I want to list records by the "date_added" desc (so most recently added 1st), and then the default would be the primary key asc (prod_id).  In a related issue, if the only order by conditional i user is "order_id asc", all of the records that don't have order_id are are showing up first -in other words, the records with assigned order_ids (1, 2,3) are showing up last. I tried making the default for "order_id" NULL, then I tried "0" and now it's blank. Please let me know what I'm doing wrong here - thanks:

SELECT prod_id, order_id, prodtype, prod_title, prod_desc, prod_price, prod2_title, prod2_desc, prod2_price, prod_image FROM products WHERE prodtype=7 ORDER BY order_id asc, date_added desc, prod_id asc