0

I have a table video with fields videoid, genre(int-foreign key), language(int-foreign key) etc. I want to get the value of genre from genre table and language from movie_languages table.

The structure of tables are given below:

video

enter image description here

genre

enter image description here

movie_languages

enter image description here

How can I join these 3 tables to get the language and genre related to each videos in video table. Also when user didn't select genre/language in the form, value 0 will be inserted to the table. Will this affect the query. I am using codeingiter and I tried with the following query and is not working.

$this->db->select('video.*,movie_languages.language,genre.genre');    
$this->db->join('genre', 'video.genre = genre.id');
$this->db->join('movie_languages', 'video.language = movie_languages.id');
$query = $this->db->get();

Please help me.

Thanks in advance.

1
  • have you tried? : this page Commented Mar 20, 2014 at 6:52

2 Answers 2

1

It will be, you need a left join in this case

Try this

$query = $this->db
->select('v.*,ml.language,g.genre')
->from('video as v')
->join('movie_languages AS ml', 'v.language = ml.id', 'left outer')
->join('genre AS g', 'v.genre = g.id', 'left outer')
->get(); 
Sign up to request clarification or add additional context in comments.

Comments

1

try this

$this->db->select('video.*,movie_languages.language,genre.genre') 
         ->from('video')
         ->join('genre', 'video.genre = genre.id')
         ->join('movie_languages', 'video.language = movie_languages.id');
$query = $this->db->get();

3 Comments

thanks for your help. But this query will not show those records where genre/language are 0.
you can add where condition ->where('genre',0)
@MohitJain you need a left join in this case. Have a look at my answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.