[英]SQL - how to get data from two tables
我有這些表:
user_id
article_id
created_at
user_id
created_at
...
我需要從兩個表中為各自的用戶獲取所有行(讓我們說user_id = 1 ),並按created_at
列對它們進行排序。 怎么做?
我試圖這樣做:
Model.find_by_sql('SELECT table_a.* FROM table_a JOIN articles ON articles.user_id = 1 WHERE table_a.user_id = 1')
但是此查詢將不起作用。
嘗試這個
SELECT table_a.* ,articles.*
FROM table_a
LEFT JOIN articles ON articles.user_id = table_a.user_id
WHERE table_a.user_id = 1
ORDER BY table_a. created_at
SELECT
table_a.*
FROM
table_a
JOIN
articles
ON
articles.user_id = table_a.user_id
WHERE
table_a.user_id = 1
ORDER BY
table_a.created_at ASC;
我將嘗試執行以下查詢: SELECT table_a.*, articles.* FROM table_a JOIN articles ON articles.user_id = table_a.user_id WHERE table_a.user_id = 1 ORDER BY table_a.created_at ASC;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.