我做错了什么。它没有回音。期望输出是苹果和梨,val好和0。首先,我将获得table_1上价格为4的所有水果,然后我加入table_2,排除任何水果标记= 4,排除香蕉,然后我加入table_3,得到val如果因子匹配56,所以苹果匹配,和梨因子不匹配,所以返回0但是我的代码没有回音,没有看到错误,但只是回音没什么。
/*
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 table_1 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'
WHERE a.price=%d
",$pri));
print_r(sql);

答案 0 :(得分:0)
在FROM之后删除(,)(SELECT DISTINCT fruit FROM table_1 WHERE price =%d) a,
/*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