0

I'm trying to search through 3 different tables however it doesn't seem to be working. I have to remove username from the users table and then it works. How can I search different columns in each table?

$query = "(SELECT name, 'talent' FROM talent WHERE name LIKE '%" . $q ."%') 
           UNION ALL
           (SELECT name, username, 'users' FROM users WHERE name LIKE '%" . $q ."%') 
           UNION ALL
           (SELECT name, 'venues' FROM venues WHERE name LIKE '%" . $q ."%')";
1
  • 1
    Every UNIONed subquery must return exactly the same number of columns. Every corresponding column must be of compatible type as well. Commented Oct 13, 2014 at 0:31

1 Answer 1

1

Your first and last UNION are returning 2 columns

The middle UNION is returning 3 columns , so removing username didn't give error

Number of columns and type of columns should be same while using UNION

Sign up to request clarification or add additional context in comments.

2 Comments

how could i do it if they don't have the same number columns and type of columns?
@user2570937, you need to give empty value or NULL value if column doesn't have value in one of the select

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.