Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-25-2012, 08:42 AM   PM User | #1
knovial07
New to the CF scene

 
Join Date: Dec 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
knovial07 is an unknown quantity at this point
data from two tables?

How i can get data from two tables at same time?
knovial07 is offline   Reply With Quote
Old 12-25-2012, 09:46 AM   PM User | #2
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,680
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
Use JOINS
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Old 12-25-2012, 11:05 AM   PM User | #3
sfdigital
New to the CF scene

 
Join Date: Sep 2012
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
sfdigital is an unknown quantity at this point
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

Last edited by sfdigital; 12-25-2012 at 11:07 AM..
sfdigital is offline   Reply With Quote
Old 12-26-2012, 11:02 PM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,248
Thanks: 59
Thanked 3,999 Times in 3,968 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Quote:
Originally Posted by sfdigital View Post
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.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Tags
affordable web designers

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:24 PM.


Advertisement
Log in to turn off these ads.