0

I have made a very simple CMS for the company I work for. We need to display articles on our index page as well as headlines above in a carousel.

All articles are setup with a table for articles and art_categories table to link articles to their respective categories. I have been able to create a query that selects all articles and that exclude the headlines like below.

SELECT SQL_CALC_FOUND_ROWS articles.*, art_categories.*
FROM articles, art_categories
WHERE articles.categoryID=art_categories.categoryID AND articles.categoryID NOT IN(8) AND art_categories.categoryID NOT IN (8)
ORDER BY publishdate DESC LIMIT {$start},5

{$start} is used for pagination. The above query functions 100% and excludes the headlines category from the general article display area.

Now the problem I have is not being able to display the headlines on the same page by selecting them from the same table via their respective categoryID

SELECT articles.*, art_categories.*
FROM articles, art_categories
WHERE articles.categoryID=art_categories.categoryID AND articles.categoryID=8 AND art_categories.categoryID=8
ORDER BY publishdate DESC LIMIT 5

Any advice would be greatly appreciated.

Thanks the issue has been resolved and question is closed

3
  • Headlines are indicated by categoryID=8? What happens with the failing query -- no output?
    – Rick James
    Commented Dec 30, 2016 at 20:41
  • There will always be headlines and we will always display 5 of them, also I'm not sure what to do with te code if the query fails... Still busy learning. Commented Jan 5, 2017 at 7:27
  • "query fails" -- with error message? missing rows? extra rows? something else?
    – Rick James
    Commented Jan 5, 2017 at 17:50

1 Answer 1

0

You can use LEFT JOIN for this:

SELECT 
     articles.*, 
     art_categories.*
FROM art_categories LEFT JOIN articles
ON articles.categoryID=art_categories.categoryID
WHERE art_categories.categoryID=8 
ORDER BY publishdate DESC LIMIT 5

as advice - avoid use *, better to list all columns by name, it also allow avoid duplicates and/or arrange aliases for them

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.