简体   繁体   中英

How I can pass this SQL Query to TypeORM?

I want to sort the closest users by a user

SELECT usuario1.nickname, usuario1.name
FROM public."user" as usuario1, public."user" as usuario2
WHERE usuario1.nickname != 'Lucas' and usuario2.nickname = 'Lucas'
ORDER BY ST_Distance(usuario1.geometry, usuario2.geometry) ASC;

You can execute raw queries in Typeorm with the created connection

import {createConnection, Connection} from "typeorm";

const connection = await createConnection({
    type: "mysql",
    host: "localhost",
    port: 3306,
    username: "test",
    password: "test",
    database: "test"
});

async function getUsers() {
   await connection.connect();

   const users = await connection.query(`
      SELECT usuario1.nickname, usuario1.name
      FROM public."user" as usuario1, public."user" as usuario2
      WHERE usuario1.nickname != 'Lucas' and usuario2.nickname = 'Lucas'
      ORDER BY ST_Distance(usuario1.geometry, usuario2.geometry) ASC
   `);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM