skip to Main Content

I get this error:

ERROR:  42601: syntax error at or near "ON"
LINE 23:       ce.image from cars_images ce ON (c.id = ce.car_id)) as images

here is my code:

   SELECT

    c.id,
    c.model,
    c.description,
    c.environment,
    c.equipment,
    c.ps,
    c.tuv,
    c.cover,
    c.brand,
    c.price,
    c.engine,
    c.fuel,
    c.firstregistration,
    c.km,
    c.color,
    c.video,
    c.type,
    c.show,
    c.sit_place,
    (select
      ce.image from cars_images ce ON (c.id = ce.car_id)) as images
    
    FROM cars c


   GROUP BY c.id

   limit 100

what I am doing wrong on my subquery ?

I have googled but not found I saw the same subquery on others but idk why my it is not working

2

Answers


  1. You are missing a join. Don’t use ON without a join.

    you probably want a WHERE instead

    select
          ce.image from cars_images ce WHERE (c.id = ce.car_id)
    
    Login or Signup to reply.
  2. Your subquery is missing a join clause. on does not make sense without a join.

    On the other hand, do keep in mind that in PostgreSQL to add a subquery in a select clause it must be a scalar subquery (it must return only one row). And query will be evaluated once per row (potentially slow).

    You could try this:

    SELECT
      c.id,
      c.model,
      c.description,
      c.environment,
      c.equipment,
      c.ps,
      c.tuv,
      c.cover,
      c.brand,
      c.price,
      c.engine,
      c.fuel,
      c.firstregistration,
      c.km,
      c.color,
      c.video,
      c.type,
      c.show,
      c.sit_place,
      (select STRING_AGG(ce.image, ', ') from cars_images where (c.id = ce.car_id)) as images    
    FROM cars c
    GROUP BY c.id
    limit 100
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search