0

私はこれらのテーブルを持っています:

table_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 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 に答える
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 に答える