2011-10-18 2 views
-1

이 오류의 원인은 무엇입니까? 어떻게 해결할 수 있습니까?오류 : 사용 된 SELECT 문의 열 수가 다릅니다. 어떻게 수정합니까?

$find = 'hello'; 
$data = $this->db->query('SELECT * FROM tour_foreign_residence WHERE name LIKE "%' . $find . '%" UNION SELECT * FROM tour_foreign WHERE name LIKE "%' . $find . '%"'); 

오류 :

A Database Error Occurred
Error Number: 1222
The used SELECT statements have a different number of columns
SELECT * FROM tour_foreign_residence WHERE name LIKE "%hello%" UNION SELECT * FROM tour_foreign WHERE name LIKE "%hello%"
Filename: D:\xampp\htdocs\system\database\DB_driver.php
Line Number: 330

+0

두 개의 별도 선택을 사용합니까? –

+1

'select *'를 사용하지 말고 원하는 열을 지정하십시오. 'union '의 다른 부분의 열은 동일한 데이터 유형을 가져야하며 같은 수의 열이어야합니다. 달성하기가 불가능한 경우 두 개의 분리 된 선택문이 필요합니다. –

+0

내 게시물을 읽으십시오 여기에 : http://stackoverflow.com/questions/7800259/select-data-from-a-database-and-other-database-related-to-the UNION 또는 무엇에서 사용해야합니까? 내 질문에 어때? –

답변

0

망가 조회에서 SELECT *를 사용합니다. 대신 데이터베이스에서 가져올 실제 열 이름을 적어 두십시오. 열 이름은 두 표 모두에서 동일해야합니다. 현재 UNION이 있으며 테이블 tour_foreign_residencetour_foreign에 다른 열이있는 것 같습니다.

+0

내 게시물을 읽으십시오 : http://stackoverflow.com/questions/7800259/select-data-from-a-database-and-other-database-related-to-the UNION 또는 무엇에서 사용해야합니까? 내 질문에 어때? –

1

UNION이 정말로 필요합니까?

UNION UNION에서는 SELECT 절에 포함 된 모든 쿼리의 수가 같아야하므로 *를 사용하지 말고 쿼리 할 필드를 명시 적으로 작성해야합니다. 또한 clumns 같은 데이터 형식이 있어야합니다. w3schools article about UNION operator에 따르면

+0

내 게시물을 읽으십시오 : http://stackoverflow.com/questions/7800259/select-data-from-a-database-and-other-database-related-to-the UNION에서 사용해야합니까? 내 질문에 어때? –

0

:이 두 가지 규칙 UNION 연산자를 사용

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.

+0

내 게시물을 읽어주십시오 : http://stackoverflow.com/questions/7800259/select-data-from-a-database-and-other-database-related-to-the UNION 또는 무엇에서 사용해야합니까? 내 질문에 어때? –

2

는 :

  1. 개수와 순서는 열을 전유의 열 데이터의
  2. 유형 통합 쿼리에서 동일해야 입수해온 호환 가능해야 함

귀하의 경우, 첫 번째 규칙은 무시됩니다. 대신 join 연산자를 사용하십시오.

+0

내 게시물을 읽으십시오 : http://stackoverflow.com/questions/7800259/select-data-from-a-database-and-other-database-related-to-the UNION에서 사용해야합니까? 내 질문에 어때? –

관련 문제