2017-10-29 10 views
1

잘못된 테이블을 검색 :중첩 SQL 쿼리 나는 두 개의 테이블이 (PostgreSQL을)

  • devices (ID (INT), 유형 (INT))
  • devices_log (ID (INT), DEVICE_ID을 (INT) (FK), 데이터 (문자열) 타입은 1-10 사이의 수이다

날짜 (문자열))는, 상기 데이터 열은 1-100 ("1 사이의 수치 ", ...,"100 ") 또는 "em ptyX는 "(X는 어떠한 문자를 나타내는"는 Z ""을 ")

항상 9 타입을 갖는 데이터 열의 수를 갖는 장치.

I는 모두 "유형 9"에 대한 데이터 필드를 업데이트 는 데이터보다 큰 50되도록 ==> 데이터 = 데이터/2가 장치 할 필요가있다.

나는 INNER JOIN 시작했습니다

select l.id 
from devices_log l 
inner join (select id from devices where type = 9) d on (l.device_id = d.id) 
where cast(data as INTEGER) > 50 

내가 가진 :

이 문은 "유형 9"장치에 대한 모든 로그를 반환
select l.id 
from devices_log l 
inner join (select id from devices where type = 9) d on (l.device_id = d.id) 

,하지만 난 WHERE 조건을 추가 할 때 이 오류 :

ERROR: invalid input syntax for integer: "emptyG"

나는 같은 오류가 발생합니다.

select id 
from devices_log 
where device_id in (select id from devices where type = 9) 
and cast(data as integer) > 50 

select id from 
(
    select id, device_id, cast (devices_log.data as integer) as int_data 
    from devices_log 
    join devices on (devices_log.device_id = devices.id 
) and type = 9) ccs where ccs.int_data > 50 

의견이 있으십니까?

덕분에 사전에

답변

2

SQL 쿼리 결과 집합, 특정 단계가 수행되지 않는 설명합니다. 나는 실제로이 문제가 Postgres에 나타나지 않는다고 생각했다. (나는 다른 데이터베이스에서 그것을 보았다). 나는이 버전으로 시작할 것 :이 문제가 해결되지 않으면

select l.id 
from devices_log l inner join 
    devices d 
    on l.device_id = d.id 
where d.type = 9 and cast(l.data as INTEGER) > 50 ; 

, 그럼 당신이이 문제를 해결할 수있는 casewhere의 :

select l.id 
from devices_log l inner join 
    devices d 
    on l.device_id = d.id 
where d.type = 9 and 
     (case when d.type = 9 then cast(l.data as INTEGER) end) > 50 ; 

casethen하지 않는 한 평가 안 조건은 참입니다.