Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    data from two tables?

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

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,861
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Use JOINS
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,919
    Thanks
    79
    Thanked 4,423 Times in 4,388 Posts
    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.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •