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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:45:17+00:00 2026-05-17T15:45:17+00:00

I have multiple tables that I want to join to get a specific result.

  • 0

I have multiple tables that I want to join to get a specific result.

Tables are:

ad_categories, ad_image, ad_data, ad_location

I need a row output for every one of the following rows in the ad_categories table, regardless if there exists data for any of these categories for a specific location when using the WHERE conditional.

mysql> SELECT type FROM ad_categories;
+---------------+
| type          |
+---------------+
| restaurants   |
| fitness       |
| funactivities |
| shopping      |
| homes         |
| men           |
+---------------+

For example:

mysql> SELECT alias, type, sha, originalname FROM ad_categories LEFT JOIN ad_data ON ad_data.cid=ad_categories.id LEFT JOIN ad_image ON ad_image.pid=ad_data.id LEFT JOIN ad_location ON ad_location.id=ad_data.lid GROUP BY type;
+--------+---------------+------------------------------------------+---------------------------------+
| alias  | type          | sha                                      | originalname                    |
+--------+---------------+------------------------------------------+---------------------------------+
| malibu | fitness       | ad8b277202f4ded274274744b3fa28f34e9f1c21 | thai_body_works.jpg             |
| malibu | funactivities | 6a226df8ff827aa020b077e9e0d48e4701ae2fca | rosenthal-the_malibu_estate.jpg |
| NULL   | homes         | NULL                                     | NULL                            |
| NULL   | men           | NULL                                     | NULL                            |
| malibu | restaurants   | 98f357dfa5bdb2eb1d480dc0e8b7156b1eecac31 | moonshadows.jpg                 |
| malibu | shopping      | 1b2ef538691569842b9f9fb6c3816673f334205a | malibu_surf_shack.jpg           |
+--------+---------------+------------------------------------------+---------------------------------+
6 rows in set (0.00 sec)

Lists all the category types I need, but I haven’t specified the location which is what I need to do. I want only one row per type, per location specified. If no data exists for the category type for that location, fill in null values for the columns other than type.

mysql> SELECT alias, type, sha, originalname FROM ad_categories LEFT JOIN ad_data ON ad_data.cid=ad_categories.id JOIN ad_image ON ad_image.pid=ad_data.id LEFT JOIN ad_location ON ad_location.id=ad_data.lid WHERE ad_location.alias='malibu' GROUP BY type;
+--------+---------------+------------------------------------------+---------------------------------+
| alias  | type          | sha                                      | originalname                    |
+--------+---------------+------------------------------------------+---------------------------------+
| malibu | fitness       | ad8b277202f4ded274274744b3fa28f34e9f1c21 | thai_body_works.jpg             |
| malibu | funactivities | 6a226df8ff827aa020b077e9e0d48e4701ae2fca | rosenthal-the_malibu_estate.jpg |
| malibu | restaurants   | 98f357dfa5bdb2eb1d480dc0e8b7156b1eecac31 | moonshadows.jpg                 |
| malibu | shopping      | 1b2ef538691569842b9f9fb6c3816673f334205a | malibu_surf_shack.jpg           |
+--------+---------------+------------------------------------------+---------------------------------+
4 rows in set (0.00 sec)

I am not a pro SQL ninja. Can someone correct me if I am building my SQL query incorrectly or educate me with a few clues on how to properly accomplish what I want to do? Thanks.

  • 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-17T15:45:18+00:00Added an answer on May 17, 2026 at 3:45 pm

    Left this alone for a while, came back to it. Here’s the answer I needed, thanks to a coworker at a new job:

    SELECT 
        alias, type, sha, originalname 
    FROM 
        ad_categories 
    LEFT JOIN 
        ad_location ON ad_location.alias = '?' 
    LEFT JOIN 
        ad_data ON ad_data.cid=ad_categories.id AND ad_location.id=ad_data.lid 
    LEFT JOIN 
        ad_image ON ad_image.pid=ad_data.id GROUP BY type;
    

    Thanks coworker 😉

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

Sidebar

Related Questions

No related questions found

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.