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 8524699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:45:45+00:00 2026-06-11T07:45:45+00:00

I have articles, stored in an article table. Some articles have one or more

  • 0

I have articles, stored in an article table. Some articles have one or more photos, stored in a photo table with an article_id specified. Some photos are ‘deactivated’ (photo.active = '0') and should not be retrieved.

I’m trying to get articles for the home page, and one photo for each article with one or more photos. Like so:

SELECT article.id, article.date, article.title, photo.filename_small 
FROM (article) 
LEFT JOIN photo ON photo.article_id=article.id 
WHERE photo.active = '1' 
GROUP BY article.id 
ORDER BY article.date desc 
LIMIT 10

(The “group by” is so that I don’t get multiple results for articles with multiple photos. It strikes me as awkward.)

When I have the WHERE photo.active = '1' like that, I only get results with a photo, which defeats the purpose of making the join a left join. The where is only relevant if the join matches the article with a photo, but it’s ruling out all articles without active photos. Any ideas?

(Yes, there are similar questions, but I’ve read a lot of them and am still struggling.)

  • 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-06-11T07:45:46+00:00Added an answer on June 11, 2026 at 7:45 am

    Two options.

    1. Put it in the join clause:

      LEFT OUTER JOIN photo ON photo.article_id=article.id AND photo.active = 1
      
    2. Explicitly allow nulls again:

      WHERE (photo.active = 1 OR photo.id IS NULL)
      

    The second seems unnecessarily complicated though as you already have the outer join. I’d recommend the first.

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

Sidebar

Related Questions

I have one table, e.g. pricerules, that have stored special prices by article for
I have a database with one of the table storing articles. For every article
In my My-SQL database, I have an article table (with columns article_id, article_name &
I have Table: ARTICLES ID | CONTENT --------------- 1 | the quick 2 |
I have read few articles about table partioning but still I am bit confused
I have 2 tables. Table 1 is 'articles' and Table 2 is 'article_categories'. When
Entity Photos belongs to Property entity and one property can have many photos. Mapping
I want to using solr for search on articles I have 3 table: Group
Let's say you have a table containing articles and you want want to display
I want store some article into database. I use php and mysql. Whether have

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.