English 中文(简体)
HELP! MySQL Join
原标题:HELP! MySQL Join Many to Many + PHP

And how to write MySQL Join and PHP?
Or you can make another? ...

实例:

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

结果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: 
_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

成果:

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
// 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;




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签