Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7084773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:20:36+00:00 2026-05-28T07:20:36+00:00

I’m having a bit of trouble getting the right results from a query. At

  • 0

I’m having a bit of trouble getting the right results from a query.

At the moment I have two tables, main_cats and products.

The result I am after is 6 records, in date order, with only one unique main_cat_id.

The basic table structures are

  • Main_cats: main_cat_id, main_cat_title
  • Products: product_id, main_cat_id, product_name, date_added.

I am hitting problems when I join the main_cat table to the products table. It seems to totally ignore the ORDER BY clause.

SELECT date_added, product_name,main_cat_title FROM ic_products p 
JOIN ic_main_cats icm on icm.main_cat_id=p.main_cat_id 
WHERE p.main_cat_id IN (1,2,12,22,6,8)
GROUP BY p.main_cat_id
ORDER BY date_added   ASC
LIMIT 6

If I leave the join out the query works but shows more than one main_cat_id and I cannot display the main_cat_title as needed.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T07:20:36+00:00Added an answer on May 28, 2026 at 7:20 am

    Your question is (at heart) a “select min/max date per group and associated fields” question.

    SELECT p.date_added, p.product_name, icm.main_cat_title
    FROM ic_products p 
    LEFT JOIN ic_products p2
     ON p.main_cat_id=p2.main_cat_id
     AND p.date_added > p2.date_added
    LEFT JOIN ic_main_cats icm ON icm.main_cat_id=p.main_cat_id 
    WHERE p2.date_added IS NULL
    AND p.main_cat_id IN (1,2,12,22,6,8)
    

    Let me explain: look at this table, being the first LEFT JOIN of the query above:

    SELECT p.date_added, p.product_name
    FROM ic_products p 
    LEFT JOIN ic_products p2
     ON p.main_cat_id=p2.main_cat_id
     AND p.date_added > p2.date_added
    WHERE p2.date_added IS NULL
    

    This joins products to itself: it produces a table with every combination of date_added pairs within each category, where the date in the first column is always greater than the date in the second.

    Since this is a left join, when the date in the first column is the smallest for that category, the date in the second will be NULL.

    So this basically selects the minimum date for each category (I assume you want the minimum date ie earliest occurence, based off your ORDER BY date_added ASC in your question — if you wanted the newest date_added you’d change the > to a < in the above join).

    The second LEFT JOIN to icm is just the one in your original question, so that we can retrieve main_cat_title.

    There is no need to LIMIT 6 here because firstly, only one row is retrieved per main_cat_id thanks to the first LEFT JOIN, and secondly, your AND p.main_cat_id IN (1,2,12,22,6,8) only selects 6 categories. So 6 categories at one row per category retrieves 6 rows. (Or at most 6; if you have no items in a particular category of course no rows will be retrieved).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have a text area in my form which accepts all possible characters from
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have a view passing on information from a database: def serve_article(request, id): served_article
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.