开发者

HELP! MySQL Join Many to Many + PHP

And how to write MySQL Join and PHP?

Or you can make another? ...

Examples:

Table 1:
movie_id | movie_title
1______  | title_movie_1
2______  | title_movie_2
3______  | title_movie_3
Table 2:
genre_id | genre_title
1______  | title_genre_1
2______  | title_genre_2
3______  | title_genre_3
Table 3:
id | movie_id | genre_1
1_ | __1____ | _1
1_ | __1____ | _2
1_ | __2____ | _1
1_ | __2____ | _3
1_ | __3____ | _1
1_ | __3____ | _3
1_ | __3____ | _2

Results PHP:

Movie: title_movie_1
Genre: 
_a href="genre/1 (id_genre)">title_genre_1 _/a>, 
_a href="genre/2">title_genre_2 _/a>

Movie: title_movie_2
Genre:开发者_C百科 
_a href="genre/1">title_genre_1 _/a>,
_a href="genre/3">title_genre_3 _/a>

Movie: title_movie_3
Genre: 
_a href="genre/1">title_genre_1 _/a>
_a href="genre/2">title_genre_2 _/a>
_a href="genre/3">title_genre_3 _/a>

I can not find do ...

Help, please! I'm waiting!

Sincerely, Areku


SELECT
  table1.movie_title AS movie_title,
  GROUP_CONCAT(table2.genre_title) AS genre_titles
FROM
  table3
INNER JOIN
  table1
  ON table1.movie_id = table3.movie_id
INNER JOIN
  table2
  ON table2.genre_id = table3.genre_id

Results:

title_movie_1 | title_genre_1,title_genre_2
title_movie_2 | title_genre_1,title_genre_3
title_movie_3 | title_genre_1,title_genre_2,title_genre_3

Php code :

<?php
// Make a MySQL Connection
mysql_connect("server", "user", "password") or die(mysql_error());
mysql_select_db("db") or die(mysql_error());

$result = mysql_query("SELECT ... FROM ...") 
or die(mysql_error());  

// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
  echo "Movie: " + $row['movie_title'];
  echo "Genre: " + $row['genres_title'];
} 

?>


SELECT t1.movie_title, t2.genre_title
FROM table_1 t1, table_2 t2, table_3 t3
WHERE t1.movie_id = t3.movie_id
AND t2.genre_id = t3.genre_id;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜