[英]Translate a MySQL query into Knex QueryBuilder syntax equivalent?
我很難將特定的MySQL查詢復制到Knex中(我使用書架作為ORM)。 直接在MySQL終端中查詢數據庫時,SQL查詢本身返回正確的數據集。
MySQL查詢:
SELECT processed_articles.*, publications.pub_name
FROM processed_articles
INNER JOIN processed_articles_trends ON processed_articles_trends.processed_article_id = processed_articles.id
INNER JOIN publications ON publications.id = processed_articles.pub_id
WHERE (processed_articles_trends.trend_id = 2);
Knex QueryBuilder:
knex.select(['publications.pub_name', 'processed_articles.*'])
.innerJoin('processed_articles_trends', 'processed_articles.id', 'processed_articles_trends.processed_article_id')
.innerJoin('publications', 'processed_articles.pub_id', 'publications.id')
.where('processed_articles_trends.trend_id', '=', 2);
我知道Knex語法不正確,但是我不確定在哪里(我懷疑Knex查詢的第1行中的數組語法...)並且我一生都無法弄清楚如何再轉換SQL。 表“ processing_articles_trends ”表示表“ processed_articles”和“ trends ”表之間的聯接表。
最初,我嘗試使用我的書架模型定義和“ withRelated”參數,但書架在幕后執行了一些巫術,這使我很難確定如何從“ 出版物 ”表中撤回我需要的字段。
感謝您提供任何見解-非常感謝。
knex('processed_articles')
.innerJoin('processed_articles_trends', 'processed_articles.id', 'processed_articles_trends.processed_article_id')
.innerJoin('publications', 'processed_articles.pub_id', 'publications.id')
.where('processed_articles_trends.trend_id', '=', 2)
.select(db.raw('processed_articles.*'))
.select('publications.pub_name')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.