mysql - SQL - Get elements from two tables, with a third table with all the rows -


i trying create sql statement allows me retrieve information 2 tables, third 1 has post_id each table , type. allow me know table post_id from.

all_posts:

id primary key, post_id (foreign key) type 

movies:

post_id type title 

music:

post_id type title band 

this sql statement used:

select a.*, b.*, c.* all_posts a, movies b, music c (a.post_id = b.id , a.type = b.type) or (a.post_id = c.id , a.type = c.type) 

the problem retrieves lot of results, , of them music table, , of them repeated.

thanks

you should use left join on both tables. way, return rows on all_posts table , corresponding details on movies , music table.

select * all_posts     left join movies b on a.post_id = b.post_id , a.type = b.type     left join music c on a.post_id = b.post_id , a.type = c.type 

Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -