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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:32:14+00:00 2026-06-17T01:32:14+00:00

I want to have some columns from some tables. I have a problem with

  • 0

I want to have some columns from some tables. I have a problem with the user. The userId can be 0, but I want to have it returned as null. When I do it with this simple union statement, the result looks like this:

SELECT 
      a.appointmentId, 
      u.prename AS user_prename, 
      u.lastname AS user_lastname, 
      a.start AS START , 
      a.end AS END , 
      w.workerId AS workerid 
FROM ios_appointments a 
     JOIN ios_user u
     JOIN ios_worker w 
     JOIN ios_partners p 
WHERE a.workerid_fk = w.workerid 
     AND w.partnerid_fk = p.partnerid 
     AND a.userid_fk = u.userid 
     AND p.code =  'DEMO6003' 
union 
select 
     a.appointmentId, 
     null, 
     null, 
     a.start AS START , 
     a.end AS END , 
     w.workerId AS workerid 
from ios_appointments a 
    JOIN ios_user u 
    JOIN ios_worker w 
    JOIN ios_partners p 
where 
    a.userid_fk = 0;

Expected result:

43  NULL    NULL    2013-01-10 02:00:00 2013-01-10 02:30:00 3

Real result:

43  NULL    NULL    2013-01-10 02:00:00 2013-01-10 02:30:00 1
43  NULL    NULL    2013-01-10 02:00:00 2013-01-10 02:30:00 2
43  NULL    NULL    2013-01-10 02:00:00 2013-01-10 02:30:00 3

Problem:
The statement returns a row for each worker that is registered in the database. How can I fix it?

Table schema:

CREATE TABLE `ios_workinghours` (
  `workingHourId` int(11) NOT NULL AUTO_INCREMENT,
  `start` time DEFAULT NULL,
  `end` time DEFAULT NULL,
  `workerid_fk` int(11) NOT NULL,
  PRIMARY KEY (`workingHourId`),
  KEY `workerid_fk` (`workerid_fk`)
)

create table ios_worker (
    workerid int not null auto_increment,
    prename varchar(255),
    lastname varchar(255),
    avatar varchar(255),
    lineup varchar(255),
    languages varchar(255),
    partnerid_fk int not null,
    primary key(workerid),
    foreign key(partnerid_fk) references ios_partners(partnerid)
)

create table ios_user (
    userid int not null auto_increment,
    prename varchar(255),
    lastname varchar(255),
    phone varchar(255),
    email varchar(255),
    password varchar(255),
    primary key (userid)
)

create table ios_partners (
    partnerid int not null auto_increment,
    code varchar(255),
    partnerName varchar(255),
    street varchar(255),
    zipCode varchar(255),
    city varchar(255),
    languages varchar(255),
    workers varchar(255),
    lineup varchar(255),
    primary key(partnerid)
)
  • 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-17T01:32:16+00:00Added an answer on June 17, 2026 at 1:32 am

    Your second query still needs the join criteria applied:

    FROM ios_appointments a 
         JOIN ios_user u
         JOIN ios_worker w 
         JOIN ios_partners p 
    WHERE a.workerid_fk = w.workerid 
         AND w.partnerid_fk = p.partnerid 
         AND a.userid_fk = u.userid 
         AND a.userid_fk = 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 2 tables with some columns and another table that I want its
I have datagridview with checkbox column, but I want, that some of the columns
Horrible title, I know. I want to have some kind of wordwrap, but obviously
I have some text which I want to have on separate lines. I try
I want to have some kind of single list that is initialized in a
I have a class and I want to have some bit masks with values
if i want to have some text show up in a calligraphy font, how
I am writing some BASH script and I want it have some error handling
I'm using Sublime Text 2 and SublimeCodeIntel plugin. I want to have some code
On an Android View, I want to have a some clickable text that works

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.