CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   MySQL (http://www.codingforums.com/forumdisplay.php?f=7)
-   -   data from two tables? (http://www.codingforums.com/showthread.php?t=284690)

knovial07 12-25-2012 08:42 AM

data from two tables?
 
How i can get data from two tables at same time?

abduraooft 12-25-2012 09:46 AM

Use JOINS

sfdigital 12-25-2012 11:05 AM

Suppose I have two tables

In table 1 I have

movie_id, movie_name

In table_2 I have

movie_id,movie_genre

Now I want to know the the names of movie which belongs to particular genre


ANSWER

SELECT movie_name
FROM table1
JOIN table2 USING (movie_id)
WHERE movie_genre = 'some_genre'

This is the example to get data from two tables

Destination Wedding Photographer

Top Wedding Photographers
Wedding Photographer Hertfordshire

Old Pedant 12-26-2012 11:02 PM

Quote:

Originally Posted by sfdigital (Post 1302073)
SELECT movie_name
FROM table1
JOIN table2 USING (movie_id)
WHERE movie_genre = 'some_genre'

This is the example to get data from two tables

That is *ONE* example. It uses non-standard SQL but is allowable in MySQL.

There are so many ways you might want to get data from two tables that it's hard to pin it down to one example.

For example, suppose you wanted a list of all movie_genre's with a sub-list of the movies associated with that genre. And you want a genre to show even if there is currenly no movie in that genre.
Code:

SELECT t1.movie_genre, t2.movie_name
FROM table1 AS t1 LEFT JOIN table2 AS t2
ON t1.movie_id = t2.movie_id
ORDER BY t1.movie_genre, t2.movie_name

And there are many other possible choices.


All times are GMT +1. The time now is 09:31 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.