liontass
liontass

Reputation: 740

Create one select query for two tables with different columns

I have two tables that contains different columns. I want to create a single query to fetch data from these two tables that where for a specific column for each table have LIKE condition with a php var named $a.

SELECT col_1 AS C FROM ekptes 
UNION
SELECT col_2 AS D FROM students
WHERE ekptes.surname OR 
students.eponimos 
LIKE  '%$a%'

The UNION operator doesn't work as I wrote the code. How I can fix it?

Upvotes: 1

Views: 41

Answers (2)

Fahmi
Fahmi

Reputation: 37473

You can try below-

  SELECT col_1 AS C FROM ekptes  WHERE ekptes.surname LIKE  '%$a%'
  UNION
  SELECT col_2  FROM students WHERE students.eponimos  LIKE  '%$a%'

Upvotes: 1

Arulkumar
Arulkumar

Reputation: 13237

Are you looking for?

SELECT col_1 AS C FROM ekptes WHERE surname LIKE '%$a%'
UNION
SELECT col_2 AS D FROM students WHERE eponimos LIKE '%$a%'

You can use the repsective table's column search in it's WHERE clause.

Upvotes: 1

Related Questions