SQL concat与列名称

时间:2014-09-03 13:57:26

标签: mysql sql concat

我想做这样的SQL请求:

SELECT p.ID, p.post_title
FROM ".$wpdb->prefix."posts AS p
INNER JOIN ".$wpdb->prefix."postmeta pm
ON pm.post_id=p.ID AND pm.meta_key='shops_list'
INNER JOIN ".$wpdb->prefix."follow AS f
ON pm.meta_value LIKE CONCAT('%',f.shop_followed_id,'%')

shop_followed_id是它不起作用的列名,但是当我使用CONCAT('%',163,'%')进行测试时,它可以正常工作。

你知道语法错误在哪里吗?

meta_value的例子:a:3:{i:0; s:3:“168”; i:1; s:2:“22”; i:2; s:2:“43”;} shop_followed_id的例子:168

3 个答案:

答案 0 :(得分:0)

向我看,你倒退了。尝试:

INNER JOIN ".$wpdb->prefix."follow AS f
ON f.shop_followed_id LIKE CONCAT('%',pm.meta_value,'%')

答案 1 :(得分:0)

对f.shop_followed_id使用Casting。

CONCAT('%',CAST(f.shop_followed_id为VARCHAR),'%')

可能是这项工作

答案 2 :(得分:0)

SELECT p.ID, p.post_title AS title, p.post_type AS post_type, p.post_date AS post_date, f.id_shop_followed, pm.meta_value
            FROM ".$wpdb->prefix."posts AS p
            INNER JOIN ".$wpdb->prefix."postmeta pm
            ON pm.post_id=p.ID AND pm.meta_key='shops_list'
            INNER JOIN ".$wpdb->prefix."follow AS f
            ON f.id_user_following = $id_user AND LOCATE(f.id_shop_followed, pm.meta_value)>0  AND f.id_shop_followed > 0
            WHERE ( p.post_type='news' OR p.post_type='promotions' OR p.post_type='product')
                AND p.post_status='publish' AND f.id_user_following = $id_user
            GROUP BY p.ID

非常感谢TuncayGöncüoğlu @Brahmanand Choudhary:我没有尝试你的解决方案,但它可能有效