0

我正在尝试使用下面的 SQL 语句从 3 个表中检索数据。但是,我在 [aren1002] 附近收到语法错误。

select
holiday_notes.*,
HOLIDAY_REF.holiday_name as holiday_name from [aren1002].[HOLIDAY_NOTES]
left join [aren1002].[HOLIDAY_REF] on holiday_notes.holiday_id=HOLIDAY_REF.holiday_id

[aren1002].[lookup].lookup_desc as type_desc from [aren1002].[HOLIDAY_NOTES]
left join [aren1002].[lookup] on holiday_notes.[type]=lookup.lookup_id

where [HOLIDAY_NOTES].delete_date is null order by [HOLIDAY_NOTES].create_date desc

我正在尝试添加来自 HOLIDAY_REF 的 holiday_name 列和来自第二个表(查找)的 type_desc。我不完全确定如何构建声明。

4

3 回答 3

3

您选择的列需要放在SELECT查询部分之后,然后将FROM子句以及任何连接条件放在下一个。在那之后,你可以把你的WHERE条款。

select
holiday_notes.*,
HOLIDAY_REF.holiday_name as holiday_name,
[aren1002].[lookup].lookup_desc,
[aren1002].[lookup].type_desc

from [aren1002].[HOLIDAY_NOTES]
left join [aren1002].[HOLIDAY_REF] on holiday_notes.holiday_id=HOLIDAY_REF.holiday_id
left join [aren1002].[lookup] on holiday_notes.[type]=lookup.lookup_id
where [HOLIDAY_NOTES].delete_date is null order by [HOLIDAY_NOTES].create_date desc
于 2013-10-02T15:38:49.650 回答
1

你有一个额外FROM的在那里。

试试这个

select
holiday_notes.*,
HOLIDAY_REF.holiday_name as holiday_name,
[aren1002].[lookup].lookup_desc as type_desc
 from [aren1002].[HOLIDAY_NOTES]
left join [aren1002].[HOLIDAY_REF] on holiday_notes.holiday_id=HOLIDAY_REF.holiday_id
left join [aren1002].[lookup] on holiday_notes.[type]=lookup.lookup_id
where [HOLIDAY_NOTES].delete_date is null order by [HOLIDAY_NOTES].create_date desc

选择语句通常遵循以下模式: SELECT 必填字段 FROM (可选)JOIN 将语句连接到其他表 WHERE 条件

于 2013-10-02T15:39:29.467 回答
0

I'm no sql guru, and I'm not at a system where I can set up a test quickly, so this may be completely and totally wrong.

But I'm pretty sure you should do all your SELECTs first, then specify the FROM and JOINs.

So I think you want something along the lines of:

select holiday_notes.*, HOLIDAY_REF.holiday_name as holiday_name, [aren1002].[lookup].lookup_desc as type_desc from [aren1002].[HOLIDAY_NOTES] left join [aren1002].[HOLIDAY_REF] on holiday_notes.holiday=HOLIDAY_REF.holiday_id left join [aren1002].[lookup] on holiday_notes.[type]=lookup.lookup_id where [HOLIDAY_NOTES].delete_date is null order by [HOLIDAY_NOTES].create_date desc

于 2013-10-02T15:47:00.217 回答