0

我有这些表:

表_a

user_id
article_id
created_at

文章

user_id
created_at
...

我需要从两个表中为各个用户获取所有行(比如说user_id=1)并按 column 对它们进行排序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')

但是这个查询不起作用。

4

3 回答 3

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
于 2012-06-20T13:19:14.013 回答
1
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;
于 2012-06-20T13:19:35.060 回答
1

我会尝试以下查询: 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;

于 2012-06-20T13:31:23.160 回答