简体   繁体   English

从mysql中的联接表获取数据

[英]Get data from joined table in mysql

Please check the image. 请检查图像。

两个表,最喜欢的表和邮政表。

Here i have 2 table. 在这里,我有2张桌子。 One is post table and another is fevorite table. 一个是邮政桌,另一个是收藏桌。 Both has primary key and post_id is FOREIGN_KEY in fevorite table. 两者都具有主键,并且收藏夹表中的post_id为FOREIGN_KEY。

Now my needs are : 现在我的需求是

  1. Select post_id list from fevorite table WHERE fevorite_by=2. 从收藏夹表WHERE fevorite_by = 2中选择post_id列表。
  2. Using this post_id list get post details (post_title,created_by, ...) from post table. 使用此post_id列表从帖子表中获取帖子详细信息(post_title,created_by等)。

I need all of those things in single query. 我需要在单个查询中所有这些东西。 Thanks in advance. 提前致谢。

Use sub query to get your result: 使用子查询获取结果:

SELECT * FROM Post WHERE post_id IN (
SELECT post_id FROM fevorite WHERE fevorite_by = 2)

Or you can do it by JOIN 或者您可以通过JOIN完成

SELECT P.*
FROM Post P
JOIN fevorite F ON F.post_id = P.post_id
WHERE F.fevorite_by = 2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM