AP aul
AP aul

Reputation: 368

How to search into two table where they have different column

is there any way I can search the data of a table where I will use a two criteria Firstname and lastname?

Table 1

Firstname | LastName | Gender

| Donald | Trump | Male|

Table 2

GFirstName | GLastName | Gender

|Hillary| Clinton | Female

|Donald | McDonald| Female

in my case I would like to Search for "Donald Trump"

and MySQL data should show me info of

table 1

 Donald | Trump| Male

I tried using this query but it gives me nothing

SELECT *
FROM table1
WHERE 'Firstname' LIKE %Donald Trump%
  OR 'LastName' LIKE %Donald Trump%
  UNION ALL
  SELECT *
  FROM table2 WHERE 'Firstname' LIKE %Donald Trump%
  OR 'LastName' LIKE %Donald Trump%

Upvotes: 1

Views: 54

Answers (2)

Ghost Worker
Ghost Worker

Reputation: 690

try

SELECT *
  FROM table1
  WHERE 
         CONCAT (Firstname, ' ',LastName)  LIKE '%Donald Trump%' 
  UNION ALL
  SELECT *
  FROM table2 WHERE CONCAT (Firstname, ' ',LastName)  LIKE '%Donald Trump%'

Upvotes: 1

Pathik Vejani
Pathik Vejani

Reputation: 4501

Change query like below, put brackets in or condition:

SELECT *
FROM table1
WHERE ('Firstname' LIKE %Donald Trump%
  OR 'LastName' LIKE %Donald Trump%)
  UNION ALL
  SELECT *
  FROM table2 WHERE ('Firstname' LIKE %Donald Trump%
  OR 'LastName' LIKE %Donald Trump%)

Upvotes: 1

Related Questions