[英]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.