MySQL Query在5.6中不起作用

时间:2017-10-14 16:44:22

标签: php mysql

SELECT 
  p.ID, p.post_name as _slug, 
  p.post_title as _title, 
  p.post_modified as _updated, 
  m1.meta_value as _symbol,
  CONVERT(SUBSTRING_INDEX(m2.meta_value,'-',-1), INTEGER) as _rank 
FROM wp_posts AS p 
  LEFT JOIN wp_postmeta m1 ON p.id = m1.post_id AND m1.meta_key = '_cc_symbol' 
  LEFT JOIN wp_postmeta m2 ON p.id = m2.post_id AND m2.meta_key = '_cc_rank' 
where 
  p.post_type='cryptocurrency' ORDER by p.ID asc LIMIT 1 OFFSET 0

此查询在MySQL软件版本中不起作用:5.6.35-81.0

  

#1064 - 您的SQL语法出错;检查与MySQL服务器版本对应的手册,以获得正确的语法   在'INTEGER附近'为_rank,FROM wp_posts AS p LEFT JOIN wp_postmeta m1   在第1行打开p.id = m1.post'

提前致谢

1 个答案:

答案 0 :(得分:1)

在MySQL中,使用unsignedsigned代替int。我更喜欢cast()convert()

CAST(SUBSTRING_INDEX(m2.meta_value, '-', -1) as unsigned) as _rank 

(在旧版本的MySQL中,convert()仅在字符集之间转换,而不是类型。)

或者更简单地说,使用静默转换:

(SUBSTRING_INDEX(m2.meta_value, '-', -1) + 0) as _rank