2016-08-04 30 views
0

我做錯了什麼。它什麼都不回聲。預期的產出是蘋果和梨,val的好和0。首先,我會得到所有水果價格4 table_1,然後我加入table_2,排除任何水果標記= 4,排除香蕉,然後我加入table_3,得到val如果因子匹配56,所以蘋果比賽,和梨因子不匹配,所以返回0.但我的代碼回聲沒有,沒有看到錯誤,但只是回聲沒有。 加入三個表mysql php,但什麼也沒有迴應

/* 
 
table_1 
 
fruit price 
 
apple 4 
 
apple 5 
 
banana 4 
 
banana 5 
 
pear 4 
 

 
table_2 
 
fruit mark 
 
apple 5 
 
apple 6 
 
banana 4 
 
banana 7 
 
pear  6 
 

 
table_3 
 
fruit factor val 
 
apple 56  good 
 
apple 60  OK 
 
banana 89  good 
 
banana 90  good 
 
pear  56  bad 
 
*/ 
 
$pri=4; 
 
$sql = $wpdb->get_results($wpdb->prepare(" 
 
\t \t \t \t SELECT 
 
\t \t \t \t b.fruit,c.val 
 
\t \t \t \t FROM table_1 a, 
 
\t \t \t \t LEFT JOIN table_2 b 
 
\t \t \t \t ON b.fruit=a.fruit and b.mark != '4' 
 
\t \t \t \t LEFT JOIN table_3 c 
 
\t \t \t \t ON c.fruit=a.fruit AND c.factor = '56' 
 
       WHERE a.price=%d 
 
\t \t \t \t ",$pri)); 
 
print_r(sql);

回答

0

只是刪除(,)FROM之後(SELECT DISTINCT水果FROM TABLE_1 WHERE價格=%d)一個,

/*table_1 
fruit price 
apple 4 
apple 5 
banana 4 
banana 5 
pear 4 

table_2 
fruit mark 
apple 5 
apple 6 
banana 4 
banana 7 
pear  6 

table_3 
fruit factor val 
apple 56  good 
apple 60  OK 
banana 89  good 
banana 90  good 
pear  56  bad 
*/ 
$pri=4; 
$sql = $wpdb->get_results($wpdb->prepare(" 
       SELECT 
       b.fruit,c.val 
       FROM (SELECT DISTINCT fruit FROM table_1 WHERE price=%d) a 
       LEFT JOIN table_2 b 
        ON b.fruit=a.fruit and b.mark != '4' 
       LEFT JOIN table_3 c 
        ON c.fruit=a.fruit AND c.factor = '56' 
       ",$pri)); 
print_r(sql); 

並且輸出是:

fruit val  
apple good 
apple good 
pear bad 
banana NULL