2013-01-04 3 views
0
notificationTable 
ID    type  typeID  userID 
============================================== 
1    comment  34   2 
2    accept  22   2 

eventTable 
ID    event  content 
=================================== 
21    post  34 
22    accept  22 

commentTable 
ID    comment  eventID 
=================================== 
34    'test'  21 

열 유형 값에 따라 다른 테이블을 조인하려고합니다.기본 테이블의 열 값에 따라 테이블 조인

SELECT * 
FROM notificationTable notification 
IF (notification.type == 'comment') 
    LEFT JOIN commentTable comment 
    ON notification.typeID = comment.ID 
ELSEIF (notification.type == 'accept') 
    LEFT JOIN evenTable event 
    ON notification.typeID = event.ID 
WHERE notification.userID = 2 

누군가가 내 의도에 대한 실제 쿼리를 알고 : 이 내 현재의 의사인가?

+0

이 MySQL의 절차를 통해 가능하다 또는 당신은 솔직히 추천 할 것입니다 –

+1

코딩으로 작업을 수행 할 수 있습니다 이것을 알아 내려고조차하지 않았습니다. 테이블 배치 방법의 구조를 변경하는 데 주력해야하는 것 같습니다. –

+0

및 원하는 결과는 무엇입니까? –

답변

2

뭔가

SELECT * 
FROM notificationTable notification 
LEFT JOIN commentTable comment 
ON (notification.typeID = comment.ID AND notification.type == 'comment') 
LEFT JOIN evenTable event 
ON (notification.typeID = event.ID AND notification.type == 'accept') 
WHERE notification.userID = 2 

하지만 강력하게 다형성의 관계를 방지하기 위해 시스템을 재 설계하는 것이 좋습니다 ... 같은

관련 문제