簡體   English   中英

獲取關系,然后從表中獲取-php&mysql

[英]get relation then get from a table - php & mysql

我有3個表,例如:post,attach,relationship。

當我選擇發布時,之后我需要將文件附加到該發布中。

現在,我必須從關系表中進行選擇。 因為附加可以添加到其他帖子中。

我可以輕松選擇帖子,那么現在選擇附件的最佳方法是什么?

post表:

+----+--------------+-------------+
| id | post_title   | post_text   |
+----+--------------+-------------+
|  1 | test title 1 | test text 1 |
|  2 | test title 2 | test text 2 |
+----+--------------+-------------+

attach表:

+----+-------------------------------+
| id | url                           |
+----+-------------------------------+
|  1 | http://xxxxxx.ir/img/logo.png |
|  2 | http://xxxxxx.ir/img/tut.png  |
+----+-------------------------------+

relation表:

+-----+-----+
| src | dst |
+-----+-----+
|   1 |   1 |
|   1 |   2 |
+-----+-----+

和我試過的SQL代碼:

SELECT dst FROM relation where src = 1 ;

然后比我內爆php:

$ids = implode( $result );

然后我最后的查詢:

SELECT * FROM attach WHERE id IN( $ids ) ;

我需要更好的方法和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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM