Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

180
Views
Js parse sql and extract table name alias

I have a sql query and i want to extract table alias。 for example:

select * from t_1 a join t_2 b on a.id = b.t_id where a.words = ? and b.words = ?

the result I expected

a: t_1, b: t_2

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You could try a regex match approach to capture every table name and alias.

var sql = "select * from t_1 join t_2 b on t_1.id = b.t_id where t_1.words = ? and b.words = ?";
var aliases = sql.match(/(?<=\b(?:from|join) )\w+(?: (?!\bjoin\b)\w+)?/g);
aliases.forEach(x => console.log((x.split(" ")[1] ? x.split(" ")[1] : x.split(" ")[0]) + ": " + x.split(" ")[0]));

Note that in general to do what you want would require a formal SQL parser. Regex alone is not suitable, and it would take hundreds of lines of code, most likely more.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!