简体   繁体   中英

How can I retrieve data from a table and using a foreign key retrieve data from another table?

I have 2 tables : movies, schedule .

Schedule : schedule_id, movie_id, start_date, start_time ;

Movies: movie_id, movie_name, movie_description ;

I'm using php , and I want to retrieve every record in schedule where start_date = 2014-11-14 which is quite simple but in the same query I want to get movie_name for every movie_id in schedule .

And if it's possible how will I access the result?

I used this before: while($row = $rs->fetch_row()) but since I want data from 2 tables how will I access it?

Something along the lines of:

$sql = "SELECT schedule.*, movies.movie_name FROM schedule JOIN movies ON schedule.movie_id = movies.movie_id WHERE schedule.start_data = '2014-11-14'";

You can leave the rest of your code like it is and $row will be ammended with the movie_name

Try researching MySQL JOIN on google ;)

The query you want is this:

SELECT * 
FROM Schedule 
    INNER Movies ON Movies.movie_id = Schedule.movie_id 
WHERE Schedule.start_date = '2014-11-14';

You can access it using the same function that you were using

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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