Jump to content

get data from 2 table that have relationship

- - - - -

  • Please log in to reply
9 replies to this topic

#1
cakka

cakka

    Programmer

  • Members
  • PipPipPipPip
  • 192 posts
hello,

i have 2 tables

first table : movie

+----------+----------------+------------+------------+-----------------+----------------+
| movie_id | movie_name | movie_type | movie_year | movie_leadactor | movie_director |
+----------+----------------+------------+------------+-----------------+----------------+
| 1 | Bruce Almighty | 5 | 2003 | 1 | 2 |
| 2 | Office Space | 5 | 1999 | 5 | 6 |
| 3 | Grand Canyon | 2 | 1991 | 4 | 3 |
+----------+----------------+------------+------------+-----------------+----------------+

second table : people

+-----------+-----------------+----------------+-------------------+
| people_id | people_fullname | people_isactor | people_isdirector |
+-----------+-----------------+----------------+-------------------+
| 1 | Jim Carrey | 1 | 0 |
| 2 | Tom Shadyac | 0 | 1 |
| 3 | Lawrence Kasdan | 0 | 1 |
| 4 | Kevin Kline | 1 | 0 |
| 5 | Ron Livingston | 1 | 0 |
| 6 | Mike Judge | 0 | 1 |
+-----------+-----------------+----------------+-------------------+


i want to display :

movie name + movie actor + movie director
Bruce Almighty + Jim Carrey + Tom Shadyac
.. + ... + ..

what the query to do this in mysql ?
i am really confuse

thanks

#2
fayyazlodhi

fayyazlodhi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 403 posts
Aren't you suppose to have a primary and a foreign key i.e. one field in first table that can relate it's records into second table so that a join can be applied?
Today is the first day of the rest of my life

#3
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
You'll have to use two joins from the movie table to the people table.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#4
cakka

cakka

    Programmer

  • Members
  • PipPipPipPip
  • 192 posts

WingedPanther said:

You'll have to use two joins from the movie table to the people table.

yes, i have do.
but i am confuse how the join code
can you give me know ?
thanks

#5
cakka

cakka

    Programmer

  • Members
  • PipPipPipPip
  • 192 posts

fayyazlodhi said:

Aren't you suppose to have a primary and a foreign key i.e. one field in first table that can relate it's records into second table so that a join can be applied?

i think no
thanks

#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
select * from movie left outer join people actors on movie.movie_leadactor = actors.people_id left outer join people directors on movie.move_director = directors.people_id

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7
cakka

cakka

    Programmer

  • Members
  • PipPipPipPip
  • 192 posts

WingedPanther said:

select * from movie left outer join people actors on movie.movie_leadactor = actors.people_id left outer join people directors on movie.move_director = directors.people_id

i have try your code, and get

+----------+----------------+------------+------------+-----------------+----------------+-----------+-----------------+----------------+-------------------+-----------+-----------------+-------------

---+-------------------+

| movie_id | movie_name     | movie_type | movie_year | movie_leadactor | movie_director | people_id | people_fullname | people_isactor | people_isdirector | people_id | people_fullname | people_isact

or | people_isdirector |

+----------+----------------+------------+------------+-----------------+----------------+-----------+-----------------+----------------+-------------------+-----------+-----------------+-------------

---+-------------------+

|        1 | Bruce Almighty |          5 |       2003 |               1 |              2 |         1 | Jim Carrey      |              1 |                 0 |         2 | Tom Shadyac     |

 0 |                 1 |

|        2 | Office Space   |          5 |       1999 |               5 |              6 |         5 | Ron Livingston  |              1 |                 0 |         6 | Mike Judge      |

 0 |                 1 |

|        3 | Grand Canyon   |          2 |       1991 |               4 |              3 |         4 | Kevin Kline     |              1 |                 0 |         3 | Lawrence Kasdan |

 0 |                 1 |

+----------+----------------+------------+------------+-----------------+----------------+-----------+-----------------+----------------+-------------------+-----------+-----------------+-------------

---+-------------------+



it's still wrong

any ideas ?
i am stuck on this code :(

#8
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
How is that wrong? What are you expecting? I thought you wanted the actor and director, which is what is being returned.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#9
cakka

cakka

    Programmer

  • Members
  • PipPipPipPip
  • 192 posts

WingedPanther said:

How is that wrong? What are you expecting? I thought you wanted the actor and director, which is what is being returned.

it was display all field that related with that
but very thanks
i have got the solution for this problem

#10
yassinebelkaid

yassinebelkaid

    Newbie

  • Members
  • PipPip
  • 20 posts
I don't see any shared key, it has to be one to make the JOIN.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users