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

  • Home
  • SEARCH
  • 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 5848859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:55:07+00:00 2026-05-22T12:55:07+00:00

I reworked my database from one user table to multiple user tables (divided per

  • 0

I reworked my database from one user table to multiple user tables (divided per role): tblStudents, tblTeachers, tblAdmin

When logging in, I didn’t want to run three queries to check if the user exists somewhere in my DB. So what I did was put together the following query with union

select s.id as id, s.email as email, s.password as password, s.role as role from tblStudents s
union
select a.id as id, a.email as email, a.password as password, a.role as role from tblAdmin a
union
select t.id as id, t.email as email, t.password as password, t.role as role from tblTeachers t

This selects the fields that are the same across all tables and outputs the results nicely for me.

So, I decided to try this and for some reason, my login form wouldn’t work. For my login form, I added a where clause which checks for the email address. I ran the query in my database app and surprisingly, when I do for example where email = "admin@admin.be" (this email exists in my database tblAdmin), it also selects a record from my students table.

With the where clause:

select s.id as id, s.email as email, s.password as password, s.role as role from tblStudents s
union
select a.id as id, a.email as email, a.password as password, a.role as role from tblAdmin a
union
select t.id as id, t.email as email, t.password as password, t.role as role from tblTeachers t
where email = "admin@admin.be"

The records both have id = 1 but I don’t understand why it would select the student record when I’m filtering on the admin email address. Why is this? Can someone explain and provide me with a better solution to this problem? I basically have one login form and need to select across multiple tables to check if the user exists in my db.

  • 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-22T12:55:07+00:00Added an answer on May 22, 2026 at 12:55 pm

    Thanks for updating the query; now we can see that the WHERE condition is only applied to the last UNIONed query. You need to either add that WHERE clause to each query, or wrap it as a subselect and apply the WHERE clause to that.

    select s.id as id, s.email as email, s.password as password, s.role as role from tblStudents s
    where email = "admin@admin.be"
    union
    select a.id as id, a.email as email, a.password as password, a.role as role from tblAdmin a
    where email = "admin@admin.be"
    union
    select t.id as id, t.email as email, t.password as password, t.role as role from tblTeachers t
    where email = "admin@admin.be"
    

    or

    SELECT * FROM (
    select s.id as id, s.email as email, s.password as password, s.role as role from tblStudents s
    union
    select a.id as id, a.email as email, a.password as password, a.role as role from tblAdmin a
    union
    select t.id as id, t.email as email, t.password as password, t.role as role from tblTeachers t
    ) foo where email = "admin@admin.be"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently reworked one of my own libraries to try out separating interface from
I am playing with class generation ( one class for a table - inheritance
I'm trying to use DbLinq with a SQLite database, but I'm running into a
I'm currently on a 4-person team tasked with the development and maintenance of a
I have figured out a lot about nhibernate, but this seems to be my
I'm getting a timeout error when trying to execute a LINQ (-to-SQL) query System.Data.SqlClient.SqlException:
It is looking increasingly like I'll have to go live before I have had
This question is language agnostic but I am a C# guy so I use
Does anyone know of any documentation by MS or someone else that lays out
I'm working now on an API for developers feature of our product. The first

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.