Skip to content Skip to sidebar Skip to footer

Error: The Used Select Statements Have A Different Number Of Columns, How Is Fix It?

What is causes this error? How can fix it? $find = 'hello'; $data = $this->db->query('SELECT * FROM tour_foreign_residence WHERE name LIKE '%' . $find . '%' UNION SELECT * FR

Solution 1:

There two main rules for using UNION operator:

  1. Count and ordering retreived columns should be equal in union queries
  2. Type of data in appropriates columns must be compatible

In your case, first rule is ignored. use join operator instead

Solution 2:

Are you sure you need an UNION?

UNION requires that all the queries that you're uniting have the same number of columns in the SELECT clause, so you shouldn't use the *, but esplicitly write the fields you need to query. Also, the clumns need to have the same data type.

Solution 3:

Dont use SELECT * in your query. Instead write down the actual column names that you want the get from the database. The column names should be same from both the tables. You currently have a UNION and it looks like that tables tour_foreign_residence and tour_foreign has different columns.

Solution 4:

According to w3schools article about UNION operator:

Each SELECT statement within the UNION must have the same number of columns. The columns must also have similar data types. Also, the columns in each SELECT statement must be in the same order.

Post a Comment for "Error: The Used Select Statements Have A Different Number Of Columns, How Is Fix It?"