2016-04-01 15 views
0

次のように私は、pgAdminで2つのサブクエリ(v1.20.0)に参加しようとしています:SQLで2つの条件付きサブクエリを結合するにはどうすればよいですか?

select * from (
    select distinct course_code "400-Level Courses", meet_time_start "Starting Time", meet_time_end "End Time", meet_day_of_week "Day", building_code "Building", building_room_no "Room" 
    from faculty_course_credit 
    left join course_schedule using (term_id, course_code, course_ref_no) 
    where (substring(course_code,4,1) = '4') and meet_time_start != '00:00' 
    ) 
inner join (
select * from (
    select distinct course_code "500-Level Courses", meet_time_start "Starting Time", meet_time_end "End Time", meet_day_of_week "Day", building_code "Building", building_room_no "Room" 
    from faculty_course_credit 
    left join course_schedule using (term_id, course_code, course_ref_no) 
    where (substring(course_code,4,1) = '5') and meet_time_start != '00:00' 
    )) 
using (building_code=building_code, building_room_no=building_room_no, meet_time_start=meet_time_start, meet_time_end=meet_time_end, meet_day_of_week=meet_day_of_week) 

私はスキーマ内の表を作成する権限を持っていない、と私は次のエラーメッセージを取得しておいてください。

ERROR: subquery in FROM must have an alias LINE 1: select * from ( ^HINT: For example, FROM (SELECT ...) [AS] foo. ********** Error **********

ERROR: subquery in FROM must have an alias SQL state: 42601 Hint: For example, FROM (SELECT ...) [AS] foo. Character: 16

いずれかの提案がありますか?

答えて

2

エラーがすべて表示されます。すべてのサブクエリまたは派生テーブルは、に別名が必要です。これは次のようになります。

SELECT * FROM (...) AS alias1 -- AS keyword is not needed, but I prefer it for readability 

これはあなたが欠けている部分です。あなたが参加している両方の派生テーブルに似た名前を持つ、そしてあなたがしJOIN ... USING()構文、その後、適切な方法を使用している場合


また、それは次のようになります。

SELECT t.col1, t.col2, t2.col1, t2.col2 -- this is to show you that names in both tables are identical 
FROM table t 
LEFT JOIN table2 t2 USING (col1, col2) 

はあなたのことを意味しますこれに平等演算子は必要ありません。場合には上記のようになりますJOIN ... ON句を使用している場合にのみ等価条件を指定:

SELECT t.*, t2.* 
FROM table t 
LEFT JOIN table2 t2 ON t.col1 = t2.col1 AND t.col2 = t2.col2 

私はあなたが両方の派生テーブルであなたの列の名前を変更していることに気付きました。 JOIN句では、外部クエリで使用できる名前を指定する必要があります。これらの名前は、最初の名前ではなく名前が変更された列名になります。

関連する問題