Postgres Inner Join Selecting a query returns an error: the column does not exist

I read the documentation and I cannot find what I am doing wrong here.

I execute this query:

SELECT *
FROM "parts"
INNER JOIN "categories" ON "categories"."id" = "parts"."category_id"
WHERE "categories"."name" = "cars"

And I get this error:

ERROR:  column "cars" does not exist
LINE 3: WHERE (categories.name = "cars")
                                 ^
********** Error **********

ERROR: column "cars" does not exist
SQL state: 42703
Character: 122

Category table:

CREATE TABLE categories
(
  id serial NOT NULL,
  name character varying(255),
  CONSTRAINT categories_pkey PRIMARY KEY (id)
)

Parts Table:

CREATE TABLE parts
(
  id serial NOT NULL,
  category_id integer,
  CONSTRAINT parts_pkey PRIMARY KEY (id)
)

Any help would be greatly appreciated.

+4
source share
1 answer

You should use single apostrophes for string constants:

SELECT *
FROM "parts"
INNER JOIN "categories" ON "categories"."id" = "parts"."category_id"
WHERE "categories"."name" = 'cars'

Duplicates middle db objects (fields, tables, etc.)

(Otherwise, they are not needed, only for additional functions, such as spaces in names, etc.)

+6
source

Source: https://habr.com/ru/post/1525292/


All Articles