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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:08:38+00:00 2026-05-24T08:08:38+00:00

A while ago, I asked for help on this SQL query . It shows

  • 0

A while ago, I asked for help on this SQL query. It shows snippets that belong to a parent category, using the category ID to tie them together. I tried to change it to show all categories, even when there’s no snippets inside but I can’t manage it.

I am sure this is not a PHP thing, as printing the result of this query does not show empty categories.

I just need help understanding this and what I need to change to make it show what I need it to.

Here’s the SQL:

SELECT c.id AS cid, c.slug AS cslug, c.name AS cname, s.id AS sid, s.name AS sname 
FROM categories AS c 
LEFT JOIN snippets AS s ON s.category = c.id 
WHERE c.live=1 AND s.live=1 AND c.company='$company' AND s.company='$company' 
ORDER BY c.name, s.name

Any tips and links to good resources to learn SQL would be appreciated too. 🙂

Here’s an example of what the query returns when run through PHP. SQL http://jsbin.com/obonal PHP Inefficient SQL Query

  • 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-24T08:08:39+00:00Added an answer on May 24, 2026 at 8:08 am

    Move the snippet condition into the on clause of the join.

    If you leave it in the where clause, result rows are required to match it, but missing rows from snippet will have null values, so s.live=1 will be false and you’ll get no match.

    In the on clause, only rows from snippet are required to match it, but with the left, matching is optional so you still get row from categories when snippet doesn’t match:

    SELECT c.id AS cid, c.slug AS cslug, c.name AS cname, s.id AS sid, s.name AS sname 
    FROM categories AS c 
    LEFT JOIN snippets AS s ON s.category = c.id AND s.live=1 AND s.company='$company' 
    WHERE c.company='$company'
    AND c.live=1 
    ORDER BY c.name, s.name
    

    The key part is the join on clause, which can be arbitrarily complex, and in the example of s.live=1 do not have to be true “join” predicates (ie don’t have to compare values between tables – they can be just tests on the row itself):

    LEFT JOIN snippets AS s ON s.category = c.id AND s.live=1 AND s.company='$company' 
    

    Note that some databases, eg mysql, allow your original syntax to still work as desired.

    EDITED Moved s.company=’$company’ into the on clause too. Thanks @Andriy

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

Sidebar

Related Questions

A while ago I asked this question: using xslt stylesheet to convert xhtml blank
I asked this question a while ago and got an answer that I thought
This question was asked quite some time ago, and while it covers possible solutions
A while ago I had a query that I ran quite a lot for
Okay so I know I asked a similar question a while ago, but this
This question is related to this initial question asked a little while ago. Now,
UPDATE 10/19/2010 I know I asked this question a while ago, but the workarounds
I asked this question a while ago. I now know it is a Bad
This is an extension of a question I asked a wee while ago which
I have question quite much related to this one I asked a while ago

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.