[英]get relation then get from a table - php & mysql
I have 3 table for example : post , attach , relation. 我有3个表,例如:post,attach,relationship。
and when I select post, after than I need get attached file to this post. 当我选择发布时,之后我需要将文件附加到该发布中。
Now I have to select from relation table. 现在,我必须从关系表中进行选择。 because attach can add to an other posts.
因为附加可以添加到其他帖子中。
I select the post easily, then now what is the best way to select attaches? 我可以轻松选择帖子,那么现在选择附件的最佳方法是什么?
the post
table: post
表:
+----+--------------+-------------+
| id | post_title | post_text |
+----+--------------+-------------+
| 1 | test title 1 | test text 1 |
| 2 | test title 2 | test text 2 |
+----+--------------+-------------+
the attach
table: 在
attach
表:
+----+-------------------------------+
| id | url |
+----+-------------------------------+
| 1 | http://xxxxxx.ir/img/logo.png |
| 2 | http://xxxxxx.ir/img/tut.png |
+----+-------------------------------+
the relation
table: relation
表:
+-----+-----+
| src | dst |
+-----+-----+
| 1 | 1 |
| 1 | 2 |
+-----+-----+
and my tried sql code: 和我试过的SQL代码:
SELECT dst FROM relation where src = 1 ;
and after than I implode in php : 然后比我内爆php:
$ids = implode( $result );
then my final query: 然后我最后的查询:
SELECT * FROM attach WHERE id IN( $ids ) ;
I need better way and SQL. 我需要更好的方法和SQL。
您可以使用JOIN从附加表中引入数据。
SELECT r.src, a.* FROM relation AS r JOIN attach AS a ON r.src=a.id WHERE r.src = 1;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.