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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:56:11+00:00 2026-05-27T10:56:11+00:00

I have joined two SQL Queries using SQL UNION Operator Example: $query1 = SELECT

  • 0

I have joined two SQL Queries using SQL UNION Operator

Example:

$query1 = "SELECT * FROM employees WHERE emp_first_name LIKE '%Donald%'";

$query2 = "SELECT * FROM employees WHERE emp_last_name LIKE '%George%'";

$final_query = $query1 . " UNION " . $query2

When I Run this $final_query, it shows the results of both queries if the first query’s result set is not empty. If the first query has empty result set there will be no result shown even if second query has some values in the database.

Can some one explain where is the problem.

Note: Don’t suggest me the use of single query, i have a reason to use two queries.

  • 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-27T10:56:12+00:00Added an answer on May 27, 2026 at 10:56 am
    mysql> create table employees (emp_first_name varchar(255), emp_last_name varchar(255)); 
    

    Query OK, 0 rows affected (0.01 sec)

    mysql> insert into employees values ('hasan','khan'),('john','doe'); 
    

    Query OK, 2 rows affected (0.00 sec) Records: 2 Duplicates: 0 Warnings: 0

    mysql> select * from employees where emp_first_name like '%nothing%' union select * From employees where emp_last_name like '%doe%';
    
    +----------------+---------------+ 
    | emp_first_name | emp_last_name |
    +----------------+---------------+ 
    | john           | doe           |
    +----------------+---------------+ 
    

    1 row in set (0.00 sec)

    Works fine for me. Either the query you posted is not the same as the one you’re executing or you have problem in your code.

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

Sidebar

Related Questions

I have a simple stored proc with two queries joined with a union: select
I have two sql tables Players and Teams joined by a PlayerTeam junction table.
I currently have two joined tables in a SQL Server database, one with news
I have a SQL table that will be be joined with two subtables. One
I have two tables that are joined together. A has many B Normally you
I have two tables that should be joined together by a foreign key relationship,
I have two columns that are joined together on certain criteria, but I would
Suppose you have two models, User and City, joined by a third model CityPermission:
I used the mapping solution from this question to have a joined component. But
i have two tables that are joined on a field rowid i need to

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.