How to do a Select * followed by a join SEA-ORM

646 Views Asked by At

I want to do a join with another table. I followed the tutorial on the site and the my code compiles but it's not performing the join and instead just selects the first table.

SELECT
"table1.col1"
"table1.col2"
"table1.col3"
FROM
  "table1"
  JOIN "table2" ON "table1"."col1" = "table2"."col1"
LIMIT
  1

It is only returning the data from table1 and not concatenating the columns where the condition for table1 and table2 is met.

I execute the query using the following code:

Entity::find()
            .from_raw_sql(Statement::from_string(DatabaseBackend::Postgres, query.to_owned()))
            .all(&self.connection)
            .await?

That returns a Vec<Model>. Is this the correct way? Also, how can I build a SQL statement using an Entity as the base which looks like SELECT * from "table1".

1

There are 1 best solutions below

0
DavidH On

After 'SELECT' (and before 'FROM') you are specifying which columns to include in the output, and you are selecting only three columns from table1 in your code.

Add the columns you want to include from table2 here, and you may get the results you want.