SQL 对 Zend 查询生成器的查询(OR、AND、ORDER BY FIELD、IS NULL)
SQL query to Zend query builder (OR, AND, ORDER BY FIELD, IS NULL)
我正在尝试更改我的 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
尝试以这种方式更改您的查询,我修改了 order by 中最后一个 prodid 之后的以下内容,并且我在所有列名称
之前设置了 table 的名称
$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)
;
我正在尝试更改我的 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
尝试以这种方式更改您的查询,我修改了 order by 中最后一个 prodid 之后的以下内容,并且我在所有列名称
之前设置了 table 的名称$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)
;