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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:01:44+00:00 2026-06-17T07:01:44+00:00

I have the following tables: CREATE TABLE `accommodations` ( `id` int(11) unsigned NOT NULL

  • 0

I have the following tables:

CREATE TABLE `accommodations` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`)
) 

CREATE TABLE `accommodations_exclude` (
  `id_accommodation` int(11) unsigned NOT NULL,
  `id_course` int(11) NOT NULL,
  UNIQUE KEY `id_course` (`id_course`,`id_accommodation`)
) 

In the accommodations table there are 4 records, in the accommodations_exclude there are many more. Now I would like to have a query that always give me all records from the accommodations table, and joined as extra field to see if the accommodation also exists in the accommodations_exclude table.

For example; in the accommodations_exclude there is one row with id_accommodation = 2, id_course = 16.

I want to have a resultset that shows me the following:

accommodation.id, accommodation.name, accommodation_exclude.id_accommodation, accommodation_exclude.id_course

1,'acco 1',null,null
2,'acco 2',2,16
3,'acco 3',null,null
4,'acco 4',null,null

The query I have right now is this one:

SELECT *
FROM accommodations a
LEFT JOIN accommodations_exclude ae ON a.id = ae.id_accommodation 
WHERE ae.id_course = 16

but this gives me only the resultset

2,'acco 2',2,16

and not the accommodations that should have null values

any idea on what i do wrong here ?

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

    Move the ae.id_course = 16 clause from WHERE to your LEFT JOIN

    SELECT *
    FROM accommodations a
    LEFT JOIN accommodations_exclude ae ON
        a.id = ae.id_accommodation
        AND ae.id_course = 16
    

    You should think of WHERE as a filter on the final resultset, anything to do with linking tables should be conditions in your JOINs.

    Your original WHERE ae.id_course = 16 was filtering out the NULL ae.id_course rows from the resultset.

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

Sidebar

Related Questions

I have the following tables CREATE TABLE `files` ( `fileid` int(11) NOT NULL AUTO_INCREMENT,
I have the following tables: CREATE TABLE `OBL2`.`item` ( `itemID` INT NOT NULL AUTO_INCREMENT
I have the following tables: CREATE TABLE `attendance_event_attendance` ( `id` int(11) NOT NULL AUTO_INCREMENT,
Say I have the following 2 tables, CREATE TABLE t1( name VARCHAR(25) NOT NULL,
I have the following tables: CREATE TABLE members ( member_id int(1) unsigned NOT NULL
I have the following mysql tables: CREATE TABLE `video` ( `video_id` int(11) unsigned NOT
With the following tables CREATE TABLE `Play` ( `id` int(11) NOT NULL AUTO_INCREMENT, `user_id`
i have following 3 tables CREATE TABLE [dbo].[dspartner]( [dspartnerid] [bigint] IDENTITY(1,1) NOT NULL, [name]
I have the following two tables: CREATE TABLE `table1` ( `ID` int(11) NOT NULL
Suppose I have the following tables: CREATE TABLE Game ( GameID INT UNSIGNED NOT

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.