mysql-如何按隐藏行子id筛选行

oogrdqng  于 2021-08-09  发布在  Java
关注(0)|答案(1)|浏览(253)

我有两张table。第一个是第二个成员的文章。我想要一个只显示成员id为给定id的记录的查询( $_GET['id'] )但不是给定id的条目 ($_GET['id']) articles表如下所示:

+----+------------+-----------------------+
| id | members_id | ArticleName           |
+----+------------+-----------------------+
|  1 |       1001 | Shirt Nologo          |
|  2 |       1001 | Nike Sneakers         |
|  3 |       1001 | Nike Sneakers for Men |
|  4 |       1031 | Adidas Shirt          |
+----+------------+-----------------------+

成员表如下所示:

+------+---------+
| id   | nick    |
+------+---------+
| 1001 | Member1 |
| 1031 | Member2 |
+------+---------+

我想要这样的东西:

+----+------------+-----------------------+
| id | members_id | ArticleName           |
+----+------------+-----------------------+
|  2 |       1001 | Nike Sneakers         |
|  3 |       1001 | Nike Sneakers for Men |
+----+------------+-----------------------+

对于这样的查询:

//$_GET['id'] == 1
SELECT t1.*,t2.nick 
FROM articles t1 
    LEFT JOIN members t2 ON t1.members_id=t2.id 
WHERE t1.id != '1' 
AND t1.members_id = t1.found_id
pobjuy32

pobjuy321#

如果我理解正确,您需要具有 id 您指定的值--但不是那个特定的值。如果是:

select a.*
from articles a 
where exists (select 1
              from articles a2
              where a2.member_id = a.member_id and a2.id = :id
             ) and
      id <> :id

相关问题