看起来MySQL的LIKE操作符的行为类似于a=操作符。
以下MySQL查询返回预期结果(1个条目):
$meta_key = \'_locality\';
$meta_value = \'The Hague\';
$post_ids = $wpdb->get_col( $wpdb->prepare(
"
SELECT post_id
FROM $wpdb->postmeta
WHERE meta_key = %s
AND meta_value LIKE %s
",
$meta_key,
$meta_value
) );
但以下返回一个空数组:
$meta_key = \'_locality\';
$meta_value = \'The\';
$post_ids = $wpdb->get_col( $wpdb->prepare(
"
SELECT post_id
FROM $wpdb->postmeta
WHERE meta_key = %s
AND meta_value LIKE %s
",
$meta_key,
$meta_value
) );
我做错了什么?
最合适的回答,由SO网友:birgire 整理而成
尝试替换:
AND meta_value LIKE %s
使用
AND meta_value LIKE \'%%%s%%\'
因此,您的SQL将变成:
AND meta_value LIKE \'%The%\'
而不是:
AND meta_value LIKE \'The\'