2013-07-02 12 views
0

MySql 쿼리의 두 테이블에서 "title"을 삽입하려고합니다. 아래의 첫 번째 쿼리를 실행하면 작동합니다. 두 번째 글자를 추가하면 '블로그'제목은 계속되지만 두 번째 '앨범'제목은 가져 오지 않습니다. 두 테이블에서 두 제목을 모두 얻는 방법에 대한 아이디어가 있습니까?하나의 MySQL 쿼리에 LEFT JOIN이 두 개임

$query = "SELECT updates.*, albums.title FROM updates "; 
$query .= "LEFT JOIN albums ON updates.ref_table = 'albums' AND updates.ref_id = albums.id "; 
$query .= "WHERE user_id = ".$user_id." ORDER BY date DESC"; 

Doen't 작업 ...

$query = "SELECT updates.*, albums.title, blog.title FROM updates "; 
$query .= "LEFT JOIN blog ON updates.ref_table = 'blog' AND updates.ref_id = blog.id "; 
$query .= "LEFT JOIN albums ON updates.ref_table = 'albums' AND updates.ref_id = albums.id "; 
$query .= "WHERE user_id = ".$user_id." ORDER BY date DESC"; 

답변

0

당신이 있다면 ... updates.ref_table = 'albums' AND updates.ref_id = albums.id

작품 : 나는 각각이 모두 이러한 조건을 확인해야합니다 동일한 이름을 가진 결과에서 둘 이상의 열을 사용하면 하나의 결과 만 얻을 수 있으므로 동일한 이름의 열 이름을 바꾸면 모두 가져올 수 있으며 의미있는 이름을 갖게되어 어떤 이름인지 알 수 있습니다.

$query = "SELECT updates.*, albums.title as albumtitle, blog.title as blogtitle FROM updates "; 
$query .= "LEFT JOIN blog ON updates.ref_table = 'blog' AND updates.ref_id = blog.id "; 
$query .= "LEFT JOIN albums ON updates.ref_table = 'albums' AND updates.ref_id = albums.id "; 
$query .= "WHERE user_id = ".$user_id." ORDER BY date DESC"; 
+0

도움을 주셔서 감사합니다. :) – Chris

관련 문제