Error syntax error at or near distinct

PostgreSQL error 42601- How we fix it by Sijin George | Sep 12, 2019 Syntax errors are quite common while coding. But, things go for a toss when it results in website errors. PostgreSQL error 42601 also occurs due to syntax errors in the database queries. At Bobcares, we often get requests from PostgreSQL […]

Содержание

  1. PostgreSQL error 42601- How we fix it
  2. What causes error 42601 in PostgreSQL?
  3. How we fix the error?
  4. Conclusion
  5. PREVENT YOUR SERVER FROM CRASHING!
  6. 10 Comments

PostgreSQL error 42601- How we fix it

by Sijin George | Sep 12, 2019

Syntax errors are quite common while coding.

But, things go for a toss when it results in website errors.

PostgreSQL error 42601 also occurs due to syntax errors in the database queries.

At Bobcares, we often get requests from PostgreSQL users to fix errors as part of our Server Management Services.

Today, let’s check PostgreSQL error in detail and see how our Support Engineers fix it for the customers.

What causes error 42601 in PostgreSQL?

PostgreSQL is an advanced database engine. It is popular for its extensive features and ability to handle complex database situations.

Applications like Instagram, Facebook, Apple, etc rely on the PostgreSQL database.

But what causes error 42601?

PostgreSQL error codes consist of five characters. The first two characters denote the class of errors. And the remaining three characters indicate a specific condition within that class.

Here, 42 in 42601 represent the class “Syntax Error or Access Rule Violation“.

In short, this error mainly occurs due to the syntax errors in the queries executed. A typical error shows up as:

Here, the syntax error has occurred in position 119 near the value “parents” in the query.

How we fix the error?

Now let’s see how our PostgreSQL engineers resolve this error efficiently.

Recently, one of our customers contacted us with this error. He tried to execute the following code,

But, this ended up in PostgreSQL error 42601. And he got the following error message,

Our PostgreSQL Engineers checked the issue and found out the syntax error. The statement in Line 5 was a mix of plain and dynamic SQL. In general, the PostgreSQL query should be either fully dynamic or plain. Therefore, we changed the code as,

This resolved the error 42601, and the code worked fine.

[Need more assistance to solve PostgreSQL error 42601?- We’ll help you.]

Conclusion

In short, PostgreSQL error 42601 occurs due to the syntax errors in the code. Today, in this write-up, we have discussed how our Support Engineers fixed this error for our customers.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

SELECT * FROM long_term_prediction_anomaly WHERE + “‘Timestamp’” + ‘”BETWEEN ‘” +
2019-12-05 09:10:00+ ‘”AND’” + 2019-12-06 09:10:00 + “‘;”)

Hello Joe,
Do you still get PostgreSQL errors? If you need help, we’ll be happy to talk to you on chat (click on the icon at right-bottom).

У меня ошибка drop table exists “companiya”;

CREATE TABLE “companiya” (
“compania_id” int4 NOT NULL,
“fio vladelca” text NOT NULL,
“name” text NOT NULL,
“id_operator” int4 NOT NULL,
“id_uslugi” int4 NOT NULL,
“id_reklama” int4 NOT NULL,
“id_tex-specialist” int4 NOT NULL,
“id_filial” int4 NOT NULL,
CONSTRAINT “_copy_8” PRIMARY KEY (“compania_id”)
);

CREATE TABLE “filial” (
“id_filial” int4 NOT NULL,
“street” text NOT NULL,
“house” int4 NOT NULL,
“city” text NOT NULL,
CONSTRAINT “_copy_5” PRIMARY KEY (“id_filial”)
);

CREATE TABLE “login” (
“id_name” int4 NOT NULL,
“name” char(20) NOT NULL,
“pass” char(20) NOT NULL,
PRIMARY KEY (“id_name”)
);

CREATE TABLE “operator” (
“id_operator” int4 NOT NULL,
“obrabotka obrasheniya” int4 NOT NULL,
“konsultirovanie” text NOT NULL,
“grafick work” date NOT NULL,
CONSTRAINT “_copy_2” PRIMARY KEY (“id_operator”)
);

CREATE TABLE “polsovateli” (
“id_user” int4 NOT NULL,
“id_companiya” int4 NOT NULL,
“id_obrasheniya” int4 NOT NULL,
“id_oshibka” int4 NOT NULL,
CONSTRAINT “_copy_6” PRIMARY KEY (“id_user”)
);

CREATE TABLE “reklama” (
“id_reklama” int4 NOT NULL,
“tele-marketing” text NOT NULL,
“soc-seti” text NOT NULL,
“mobile” int4 NOT NULL,
CONSTRAINT “_copy_3” PRIMARY KEY (“id_reklama”)
);

CREATE TABLE “tex-specialist” (
“id_tex-specialist” int4 NOT NULL,
“grafik” date NOT NULL,
“zarplata” int4 NOT NULL,
“ispravlenie oshibok” int4 NOT NULL,
CONSTRAINT “_copy_7” PRIMARY KEY (“id_tex-specialist”)
);

CREATE TABLE “uslugi” (
“id_uslugi” int4 NOT NULL,
“vostanavlenia parola” int4 NOT NULL,
“poterya acaunta” int4 NOT NULL,
CONSTRAINT “_copy_4” PRIMARY KEY (“id_uslugi”)
);

ALTER TABLE “companiya” ADD CONSTRAINT “fk_companiya_operator_1” FOREIGN KEY (“id_operator”) REFERENCES “operator” (“id_operator”);
ALTER TABLE “companiya” ADD CONSTRAINT “fk_companiya_uslugi_1” FOREIGN KEY (“id_uslugi”) REFERENCES “uslugi” (“id_uslugi”);
ALTER TABLE “companiya” ADD CONSTRAINT “fk_companiya_filial_1” FOREIGN KEY (“id_filial”) REFERENCES “filial” (“id_filial”);
ALTER TABLE “companiya” ADD CONSTRAINT “fk_companiya_reklama_1” FOREIGN KEY (“id_reklama”) REFERENCES “reklama” (“id_reklama”);
ALTER TABLE “companiya” ADD CONSTRAINT “fk_companiya_tex-specialist_1” FOREIGN KEY (“id_tex-specialist”) REFERENCES “tex-specialist” (“id_tex-specialist”);
ALTER TABLE “polsovateli” ADD CONSTRAINT “fk_polsovateli_companiya_1” FOREIGN KEY (“id_companiya”) REFERENCES “companiya” (“compania_id”);

ERROR: ОШИБКА: ошибка синтаксиса (примерное положение: “”companiya””)
LINE 1: drop table exists “companiya”;
^

Источник

Почему я получаю эту ошибку? Мне нужно выбрать оба как разные, но я здесь неправильно кодирую?

ERROR: syntax error at or near "DISTINCT"

SELECT DISTINCT(mfin_score), DISTINCT(empirica_score ) from account_details

6 ответов

Лучший ответ

Ты можешь сделать:

select distinct mfin_score, empirica_score
  from account_details

Ключевое слово distinct не является функцией. Это ключевое слово, указывающее, что вам нужны только distinct кортежи в вашем наборе результатов.


5

Pablo Santa Cruz
18 Сен 2014 в 16:38

DISTINCT — это KEYWORD, а не FUNCTION, поэтому будет лучше, если вы попробуете

SELECT DISTINCT mfin_score, empirica_score from account_details


4

Satya
18 Сен 2014 в 16:38

Просто используйте это

ВЫБЕРИТЕ DISTINCT mfin_score, empirica_score из account_details


2

user262503
18 Сен 2014 в 16:38

Правильный синтаксис ключевого слова DISTINCT:

SELECT DISTINCT column_name,column_name
FROM table_name;

Так что вы можете написать

SELECT DISTINCT mfin_score, empirica_score from account_details

Вместо того

SELECT DISTINCT(mfin_score), DISTINCT(empirica_score ) from account_details


2

Deb
18 Сен 2014 в 16:40

Если вам нужны отдельные пары mfin и empirica:

select distinct mfin_score, empirica_score
  from account_details

Если вам нужен отдельный mfin и отличный empirica, вам нужно сделать что-то другое:

select distinct 'MFIN' As code, mfin_score
 from account_details
union all
select distinct 'EMP' As code, empirica_score
from account_details

Возможно, вам придется проверить синтаксис postgresql для псевдонима, не уверен, что он такой же, как oracle


2

SeraphimFoA
18 Сен 2014 в 17:20

Для других, таких как я, которые получили эту ошибку — DISTINCT должен идти сразу после SELECT (перед любыми столбцами). И будет дедуплицировать комбинацию всех предоставленных столбцов.


2

Kyle Venn
26 Фев 2022 в 20:34

Почему я получаю эту ошибку? Мне нужно выбрать оба эти параметра в отдельности, но Im я кодирует это неправильно здесь?

ERROR: syntax error at or near "DISTINCT"

SELECT DISTINCT(mfin_score), DISTINCT(empirica_score ) from account_details

Вы можете сделать:

select distinct mfin_score, empirica_score
from account_details

Ключевое слово distinct не является функцией. Это ключевое слово, чтобы указать, что вы хотите вставлять только теги distinct в ваш результирующий набор.

DISTINCT является KEYWORD не a FUNCTION, поэтому лучше попробовать

SELECT DISTINCT mfin_score, empirica_score from account_details

Если вам нужны отдельные пары mfin и empirica:

select distinct mfin_score, empirica_score
from account_details

Если вам нужен отдельный mfin и отдельная эмпирика, вам нужно сделать что-то другое:

select distinct 'MFIN' As code, mfin_score
from account_details
union all
select distinct 'EMP' As code, empirica_score
from account_details

вам может потребоваться проверить синтаксис postgresql для Alias, не уверен, что он такой же, как oracle

Правильный синтаксис ключевого слова DISTINCT

SELECT DISTINCT column_name,column_name
FROM table_name;

Итак, вы можете написать

SELECT DISTINCT mfin_score, empirica_score from account_details

вместо

SELECT DISTINCT(mfin_score), DISTINCT(empirica_score ) from account_details

просто используйте это

SELECT DISTINCT mfin_score, empirica_score из account_details

Введите минимум 50 символов

Понравилась статья? Поделить с друзьями:
  • Error syntax error at or near call
  • Error syncing pod skipping
  • Error synchronizing data with database
  • Error synchronizing after initial wipe timed out waiting for object
  • Error sync firefox