Zend Framework 1 Db Multiple where语句

时间:2014-10-20 09:53:38

标签: php mysql sql zend-framework

使用以下变量$select进行所有必要的配置并且工作正常。在同一个表上过滤数据的任务如下:

例如表格values,其字段为:

  • id object_type object_value
  • 1 ----------- ------------ 8 1
  • 1 ----------- ------------ 5 300
  • 2 ----------- ------------ 8 0
  • 2 ----------- ------------ 5 500
  • 3 ----------- ------------ 8 1
  • 3 ----------- ------------ 5 400
  • 4 ----------- ------------ 8 1
  • 4 ----------- ------------ 2 10 ...

值必须按如下方式排序:

  • 当object_type = 8时,仅选择object_value = 1
  • 当object_type = 5时,仅选择object_value> = 400例如。

尝试使用 Zend_Db_select union(),但这似乎不起作用。任何想法都会非常有帮助。使用joins似乎也不是一个选项。不能实现case

更新 应用查询应该对数据和输出进行排序:

  • id object_type object_value
  • 3 ----------- ------------ 8 1
  • 3 ----------- ------------ 5 400

解释 例如,id = 1有2行

  • object_type的= 8
  • object_value = 1

  • object_type的= 5
  • object_value = 300

当object_type = 8时,查询检查id是否为number,object_type必须为= 1,当object_type = 5时,object_value必须为> = 400,因此在以下情况id为1时,将不显示其值。只有编号为3的id匹配搜索的查询。希望有一点解释,如果没有,我会在后面添加具体对象的实验值。

1 个答案:

答案 0 :(得分:0)

SQL说,你想要的是这样的:

select a.*, b.*  
from MyTable a
join MyTable b on b.id = a.id
where a.object_type = 8 and a.object_value = 1
and b.object_type = 5 and b.object_value >= 400

如果你想获得像这样的结果,你只需要获得一个好的where条件,加上一个连接条件。

//your program

$select = $this->_table->select()
               ->from(array('a' => 'MyTable'),
               ->joinLeft(array('b' => 'MyTable'), 'b.id = a.id', array())

$_where = array()
$_where[] = array('object_type= ' . "8", 'object_value = '. "1");
$_where[] = array('object_type= ' . "5", 'object_value >= '. "400");

// you can do a loop after the first element if 
// you have more conditions to put in OR
$select->where($_where[0]);
$select->orWhere($_where[1]);

//... 

或者,如果您正在进行更新

 _dbTable->update($_data, $_where);

(输入$ _data,无论你要更新什么) 添加

可能会很不错
$select->order(...);

取决于你想做什么!

相关问题