sql >> Database teknologi >  >> RDS >> Mysql

Hvordan henter man almindelige poster i den samme databasetabel gennem en enkelt SQL-forespørgsel?

Dette er en raw-sql måde at trække det ud, du vil have, tror jeg:

select * from articles_users where article_id in (select articles.id from articles inner join articles_users on articles_users.article_id = articles.id where user_id = 1) and user_id = 2;

Hvor du kan erstatte bruger-id'er i Rails:

ArticlesUser.find_by_sql(["select * from articles_users where article_id in (select articles.id from articles inner join articles_users on articles_users.article_id = articles.id where user_id = ?) and user_id = ?", @user1.id, @user2.id])

Eller for flere id'er::

ArticlesUser.find_by_sql(["select * from articles_users where article_id in (select articles.id from articles inner join articles_users on articles_users.article_id = articles.id where user_id = ?) and user_id IN (?)", @user1.id, [@user2.id,@user3.id]])

Så fra eksempeldata (fra dine andre spørgsmål):

mysql> select * from articles_users;
+----+---------+------------+
| id | user_id | article_id |
+----+---------+------------+
|  1 |       1 |          1 |
|  2 |       1 |          2 |
|  3 |       1 |          3 |
|  4 |       2 |          1 |
|  5 |       2 |          2 |
|  6 |       3 |          1 |
|  7 |       3 |          3 |
|  8 |       4 |          4 |
+----+---------+------------+
8 rows in set (0.00 sec)

Det vil returnere værdier således:

mysql> select * from articles_users where article_id in (select articles.id from articles inner join articles_users on articles_users.article_id = articles.id where user_id = 1) and user_id = 2;
+----+---------+------------+
| id | user_id | article_id |
+----+---------+------------+
|  4 |       2 |          1 |
|  5 |       2 |          2 |
+----+---------+------------+
2 rows in set (0.00 sec)

mysql> select * from articles_users where article_id in (select articles.id from articles inner join articles_users on articles_users.article_id = articles.id where user_id = 1) and user_id = 3;
+----+---------+------------+
| id | user_id | article_id |
+----+---------+------------+
|  6 |       3 |          1 |
|  7 |       3 |          3 |
+----+---------+------------+
2 rows in set (0.00 sec)

Eller for flere bruger-id'er:

mysql> select * from articles_users where article_id in (select articles.id from articles inner join articles_users on articles_users.article_id = articles.id where user_id = 1) and user_id in (2,3);
+----+---------+------------+
| id | user_id | article_id |
+----+---------+------------+
|  4 |       2 |          1 |
|  5 |       2 |          2 |
|  6 |       3 |          1 |
|  7 |       3 |          3 |
+----+---------+------------+
4 rows in set (0.00 sec)

Du har bedt om en sql-måde - men der er næsten helt sikkert en uoverskuelig måde at gøre dette på... men dette burde få dig i gang, og du kan omstrukturere det herfra.




  1. MySQL:fjern på hinanden følgende duplikerede værdier

  2. MySQL CREATE FUNCTION Syntaks

  3. Sådan opretter du Stored Procedure i MySQL

  4. Oracle 12.2.0.1 kommer i 2016