我正在尝试更改我的 SQL 查询:
SELECT link FROM shop_product_videos WHERE brand='Goddess' AND ( prodid='' OR prodid='' OR prodid IS NULL)
ORDER BY FIELD(prodid='' OR prodid IS NULL, 1, 0), prodid
LIMIT 1
这是我到目前为止尝试过的:
$query = $this->select('link')
->from('shop_product_videos')
->where('brand=?', $brand)
->where('prodid=?', $prodid)
->orWhere('prodid=""')
->orWhere('prodid is null')
->order(new Zend_Db_Expr("FIELD(prodid='' OR prodid IS NULL, 1, 0), prodid)"))
->limit(1)
;
return $this->getAdapter(false)->fetchAll($query);
但我不断收到错误:
An error occurred
Exception information:
Message: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') LIMIT 1' at line 2
最佳答案
尝试以这种方式更改您的查询,我修改了顺序中最后一个 prodid 之后的 ),并且我在所有列名称之前设置了表的名称
$query = $this->select('link')
->from('shop_product_videos')
->where('shop_product_videos.brand=?', $brand)
->where('shop_product_videos.prodid=?', $prodid)
->orWhere('shop_product_videos.prodid=""')
->orWhere('shop_product_videos.prodid is null')
->order(new Zend_Db_Expr("FIELD(shop_product_videos.prodid='' OR shop_product_videos.prodid IS NULL, 1, 0), shop_product_videos.prodid"))
->limit(1)
;
关于php - 对 Zend 查询生成器的 SQL 查询(OR、AND、ORDER BY FIELD、IS NULL),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45587977/