SQL FROM子句中的语法错误

时间:2021-06-13 15:43:43

I think my syntax is correct. Whats wrong with my syntax? I need to use it as search item.

我认为我的语法是正确的。我的语法有什么问题?我需要将它用作搜索项目。

But still it shows the error

但它仍显示错误

Syntax error in FROM clause

FROM子句中的语法错误

procedure TForm9.Button1Click(Sender: TObject);
begin
 ADOQuery1.Close;

ADOQuery1.SQL.Clear;
ADOQuery1.SQL.Add ('SELECT DAT20303.NUM,DAT20303.NAMA,DAT20303.MATRIC_ID,DAT20303.SUBJEK,DAT20303.M1,DAT20303.M2,DAT20303.M3,DAT20303.M4,DAT20303.M5,DAT20303.M6,DAT20303.M7, STUDENT.SEKSYEN,STUDENT.MATRIC_ID');
 ADOQuery1.SQL.Add ('FROM STUDENT');
 ADOQuery1.SQL.Add ('JOIN DAT20303');
 ADOQuery1.SQL.Add ('ON DAT20303.MATRIC_ID = STUDENT.MATRIC_ID');
 ADOQuery1.SQL.Add ('WHERE SEKSYEN = ' + ComboBox2.Text);



ADOQuery1.Open; {open query + display data}
end;

2 个解决方案

#1


4  

You must not use only JOIN within your query.

您不能仅在查询中使用JOIN。

This

这个

SELECT *
FROM STUDENT
  JOIN DAT20303 ON DAT20303.MATRIC_ID = STUDENT.MATRIC_ID

leads to

导致

Syntax error in FROM clause

FROM子句中的语法错误

but this will do fine

但这样做会很好

SELECT *
FROM STUDENT
  LEFT JOIN DAT20303 ON DAT20303.MATRIC_ID = STUDENT.MATRIC_ID

or this one

或者这个

SELECT *
FROM STUDENT
  INNER JOIN DAT20303 ON DAT20303.MATRIC_ID = STUDENT.MATRIC_ID

Also have a look at this question here on SO

在这里也看看这个问题

#2


4  

Do you in fact have a STUDENT table? You refer to both STUDENT and LSTUDENT in your SELECT, but only use STUDENT in the FROM clause. Which one is it?

你实际上有学生桌吗?您在SELECT中引用STUDENT和LSTUDENT,但仅在FROM子句中使用STUDENT。哪一个?

Also, at a minimum, you need to quote the content of your WHERE condition.

此外,您至少需要引用WHERE条件的内容。

ADOQuery1.SQL.Add ('WHERE SEKSYEN = ' + QuotedStr(ComboBox2.Text));

Much better though would be to stop concatenating SQL in your code, and use parameterized queries instead. It's not only much safer against SQL injection, but it allows the database engine to compile and cache the query content, making it execute much more quickly if you're using the same statement more than one time with different parameter values.

但更好的方法是停止在代码中连接SQL,并使用参数化查询。它不仅对SQL注入更安全,而且它允许数据库引擎编译和缓存查询内容,如果您使用不同参数值多次使用相同的语句,则可以更快地执行。

procedure TForm9.Button1Click(Sender: TObject);
begin
  ADOQuery1.Close;

  ADOQuery1.SQL.Clear;
  ADOQuery1.SQL.Add('SELECT DAT20303.NUM, DAT20303.NAMA, DAT20303.MATRIC_ID,');
  ADOQuery1.SQL.Add('DAT20303.SUBJEK, DAT20303.M1, DAT20303.M2, DAT20303.M3,');   
  ADOQuery1.SQL.Add('DAT20303.M4, DAT20303.M5, DAT20303.M6,');    
  ADOQuery1.SQL.Add('DAT20303.M7, STUDENT.SEKSYEN, LSTUDENT.MATRIC_ID');
  ADOQuery1.SQL.Add ('FROM STUDENT');
  ADOQuery1.SQL.Add ('JOIN DAT20303');
  ADOQuery1.SQL.Add ('ON DAT20303.MATRIC_ID = STUDENT.MATRIC_ID');
  ADOQuery1.SQL.Add ('WHERE SEKSYEN = :SekSyen');
  ADOQuery1.Parameters.ParamByName('SekSyen').AsString := ComboBox1.Text;
  ADOQuery1.Open; {open query + display data}
end;

And please don't be afraid to use whitespace (spaces and line breaks) to make your SQL more readable! I've cleaned it up somewhat in my answer to do so. When it's easier to read, it's easier to understand (and maintain later).

请不要害怕使用空格(空格和换行符)来使您的SQL更具可读性!在我的答案中,我已经清理了一些。当它更容易阅读时,它更容易理解(并在以后维护)。

#1


4  

You must not use only JOIN within your query.

您不能仅在查询中使用JOIN。

This

这个

SELECT *
FROM STUDENT
  JOIN DAT20303 ON DAT20303.MATRIC_ID = STUDENT.MATRIC_ID

leads to

导致

Syntax error in FROM clause

FROM子句中的语法错误

but this will do fine

但这样做会很好

SELECT *
FROM STUDENT
  LEFT JOIN DAT20303 ON DAT20303.MATRIC_ID = STUDENT.MATRIC_ID

or this one

或者这个

SELECT *
FROM STUDENT
  INNER JOIN DAT20303 ON DAT20303.MATRIC_ID = STUDENT.MATRIC_ID

Also have a look at this question here on SO

在这里也看看这个问题

#2


4  

Do you in fact have a STUDENT table? You refer to both STUDENT and LSTUDENT in your SELECT, but only use STUDENT in the FROM clause. Which one is it?

你实际上有学生桌吗?您在SELECT中引用STUDENT和LSTUDENT,但仅在FROM子句中使用STUDENT。哪一个?

Also, at a minimum, you need to quote the content of your WHERE condition.

此外,您至少需要引用WHERE条件的内容。

ADOQuery1.SQL.Add ('WHERE SEKSYEN = ' + QuotedStr(ComboBox2.Text));

Much better though would be to stop concatenating SQL in your code, and use parameterized queries instead. It's not only much safer against SQL injection, but it allows the database engine to compile and cache the query content, making it execute much more quickly if you're using the same statement more than one time with different parameter values.

但更好的方法是停止在代码中连接SQL,并使用参数化查询。它不仅对SQL注入更安全,而且它允许数据库引擎编译和缓存查询内容,如果您使用不同参数值多次使用相同的语句,则可以更快地执行。

procedure TForm9.Button1Click(Sender: TObject);
begin
  ADOQuery1.Close;

  ADOQuery1.SQL.Clear;
  ADOQuery1.SQL.Add('SELECT DAT20303.NUM, DAT20303.NAMA, DAT20303.MATRIC_ID,');
  ADOQuery1.SQL.Add('DAT20303.SUBJEK, DAT20303.M1, DAT20303.M2, DAT20303.M3,');   
  ADOQuery1.SQL.Add('DAT20303.M4, DAT20303.M5, DAT20303.M6,');    
  ADOQuery1.SQL.Add('DAT20303.M7, STUDENT.SEKSYEN, LSTUDENT.MATRIC_ID');
  ADOQuery1.SQL.Add ('FROM STUDENT');
  ADOQuery1.SQL.Add ('JOIN DAT20303');
  ADOQuery1.SQL.Add ('ON DAT20303.MATRIC_ID = STUDENT.MATRIC_ID');
  ADOQuery1.SQL.Add ('WHERE SEKSYEN = :SekSyen');
  ADOQuery1.Parameters.ParamByName('SekSyen').AsString := ComboBox1.Text;
  ADOQuery1.Open; {open query + display data}
end;

And please don't be afraid to use whitespace (spaces and line breaks) to make your SQL more readable! I've cleaned it up somewhat in my answer to do so. When it's easier to read, it's easier to understand (and maintain later).

请不要害怕使用空格(空格和换行符)来使您的SQL更具可读性!在我的答案中,我已经清理了一些。当它更容易阅读时,它更容易理解(并在以后维护)。