Mysql – Selecting data from one table via categoryID field and displaying in different areas whilst excluding categoryID for one area

MySQLPHPselect

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

Best Answer

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