我有兩個要加入的表。它可以在沒有 where 條件的情況下作業。添加 where 條件后,我在附近遇到語法錯誤a(我給 table1 一個替代名稱)。據我了解,語法看起來正確嗎?
我的查詢
select * from table1 where date >= '2020-10-01' and date <= '2020-10-31' a
left join table2 b where registered >= '2020-10-01' and registered <= '2020-10-31' b
on a.id = cast(b.id as varchar)
uj5u.com熱心網友回復:
一些問題:
where追蹤所有表(及其連接條件)- 別名緊跟在表名之后
應用這兩個更正和一些格式:
select *
from table1 a
left join table2 b on a.id = cast(b.id as varchar)
and registered >= '2020-10-01' and registered <= '2020-10-31'
where date >= '2020-10-01' and date <= '2020-10-31'
按照慣例,描述對連接行(通常是鍵)的訪問的連接條件首先被編碼,然后過濾條件(只涉及連接表中的列)最后被編碼。
這可以稍微簡化between為:
select *
from table1 a
left join table2 b on a.id = cast(b.id as varchar)
and registered between '2020-10-01' and '2020-10-31'
where date between '2020-10-01' and '2020-10-31'
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/450553.html
標籤:PostgreSQL
下一篇:按年份的最高發生率對事件進行排序
