Zend Db Select add subquery to FROM part - zend-framework

I try to build a query with subquery in FROM part using the Zend_Db_Select. Im looking for somthing like this:
SELECT COUNT(row_1) AS count_row FROM (SELECT row,row2,... FROM table WHERE row= ...) AS temp WHERE row = 0)
So I try to do it like this:
$oSubSelect =
$this->select()
->setIntegrityCheck(false)
->from('table',
array(
'row_id'
)
)
->where(PRFX.'table.id = '.PRFX.'table2.id')
->from(PRFX.'table2',array('row','row2'));
$this->select(false)
->setIntegrityCheck(false)
->from(new Zend_Db_Expr($oSubSelect).' AS temp',
array(
'COUNT(row_id) AS row_count',
)
);
But this gives me an error message.
Best regards.

Ok I fix this. The problem was in
->from(new Zend_Db_Expr($oSubSelect).' AS temp',
Should be:
->from(new Zend_Db_Expr('('.$oSubSelect.')'),

Related

what is the correct syntax for squeryl to write or and?

What is the correct syntax to write sql like this squeryl:
select *
from table
where
(colA = 'value1' or colA = 'value2' )
and colB = 'value3'
???
The example under the Nesting Sub Queries suggests that you should use simple and and or. Have you tried this? I mean something straightforward like:
table.where(t =>
((t.colA === "value1") or (t.colA === "value2"))
and (t.colB === "value3"))
Code like this seems to work fine for me.

Why Zend framework Db Table put an asterisk before the table name on my query?

I have a problem with a query like this:
$sSqlAux = $this->select()->setIntegrityCheck(false)
->from(array("a_aux" => $this->_name), "id_a", $this->_schema)
->join(array("b_aux"=> "b"), "a_aux.id_a = b_aux.id_b", array(), $this->_schema)
->join(array("c_aux"=> "c"), "a_aux.id_inscrito = c_aux.id_c", array(), $this->_schema)
->where("b_aux.id_b = ?", $this->idB)
;
$sSql = $this->select()->setIntegrityCheck(false)
->from(array("a" => $this->_name), "a.id_a, a.campo_a", $this->_schema)
->joinLeft(array("b" => "b"), "a.id_a = b.id_b", "b.campo_b", $this->_schema)
->joinLeft(array("c" => "c"), "b.id_b = c.id_c", "c.campo", $this->_schema)
->where("c.campo_c = ?", "string")
->where("a.id_a IN (?)", new Zend_Db_Expr($sSqlAux))
->order("c.campo_c")
;
and to return the data, an asterisk is added before the name of the table, in this case, in one of my left joins, table b (LEFT JOIN schema.*b ON a.id_a = b.id_b) like the following example
SELECT
a.id_a,
a.campo_a
b.campo_b,
c.campo_c
FROM schema.a a
LEFT JOIN schema.*b ON a.id_a = b.id_b
LEFT JOIN schema.c c ON a.id_a = c.id_c
WHERE (c.campo_c = 'string')
AND (a.id_a IN (
SELECT a_aux.id_a
FROM schema.a a_aux
INNER JOIN schema.b b_aux ON a_aux.id_a = b_aux.id_b
INNER JOIN schema.c c_aux ON a_aux.id_a = c_aux.id_c
WHERE (a_aux.id_segundo_id = 5321)))
ORDER BY c.campo ASC
)
These are just examples of the problem I'm having, if error in logic or syntax error was in my typing. The real problem is that asterisk added by zend before the table name in the join.
If someone has understood the problem and have a solution that you can share would be of great help.
I'm using zend 1
thank's!
I had the same problem and solved it.
I was on an ORACLE Database and all my table names and columns names must be in capital.
So try B_AUX instead of b_aux if you're on ORACLE DB too.

How to write Zend db select with OR condition inside AND condition

Can someone guide me to write a query like below using Zend db select :
SELECT `tbl_md_users`.*
FROM `tbl_md_users`
WHERE
user_type <> 'TYPE1')
AND (first_name LIKE '%tom%' OR last_name LIKE '%tom%' OR user_name LIKE '%tom%')
If you wanted
SELECT `tbl_md_users`.*
FROM `tbl_md_users`
WHERE
user_type <> 'TYPE1')
AND (first_name LIKE '%tom%' OR first_name LIKE '%dick%' OR first_name LIKE '%harry%')
then the first answer doesn't work
I used Zend_Db_Expr instead
$likeTom = new Zend_Db_Expr("first_name LIKE '%tom%'");
$likeDick = new Zend_Db_Expr("first_name LIKE '%dick%'");
$likeHarry = new Zend_Db_Expr("first_name LIKE '%harry%'");
$query = $database->select ()
->from ('tbl_md_users')
->where ('user_type <> ?', 'TYPE1')
->where ("{$likeTom} OR {$likeDick} OR {$likeHarry}");
$query = $database->select ()
->from ('tbl_md_users')
->where ('user_type <> ?', 'TYPE1')
->where ("first_name LIKE '%?%' OR last_name LIKE '%?%' OR user_name LIKE '%?%'", 'tom');
The current, up-to-date solution is to call nest() and unnest() in the where clause:
$select->where
->nest()
->equalTo('column1', 1)
->or
->equalTo('column2', 2)
->unnest()
->and
->equalTo('column3', 3);
I think there is no way to use Zend_Db_Select for this. You could use
$this->table->getAdapter()->quoteInto()
To write an custom query.

Zend_Db_Select: LEFT JOIN on a subselect

I have a query, that does a LEFT JOIN on a subselect. This query is run in a high load environment and performs within the set requirements. The query (highly simplified) looks like:
SELECT
table_A.pKey
, table_A.uKey
, table_A.aaa
, table_B.bbb
, alias_C.ccc
, alias_C.ddd
FROM table_A
INNER JOIN table_B ON table_A.pKey = table_B.pKey
LEFT JOIN (
SELECT
table_X.pKey
, table_X.ccc
, table_Y.ddd
FROM table_X
INNER JOIN table_Y ON table_X.pKey = table_Y.pKey
) AS alias_C ON table_A.uKey = alias_C.pKey;
(for various reasons, it is not possible to rewrite the subselect as a (direct) LEFT JOIN).
Now, I cannot get the LEFT JOIN on subselect to work with Zend_Db_Select. I've tried everything I could come up with, but it does not work.
So my question is:
Is it not possible to do a query as described above with Zend_Db_Select?
What syntax do I need to get it to work within Zend Framework?
I think that it should work like this:
$subselect = $db->select->from(array('x' => 'table_X'), array('x.pKey', 'x.ccc', 'y.ddd'), 'dbname')
->join(array('Y' => 'table_Y'), 'x.pkey = y.pkey', array(), 'dbname');
$select = $db->select->from(array('a' => 'table_A'), array(/*needed columns*/), 'dbname')
->join(array('b' => 'table_B'), 'a.pkey = b.pkey', array(), 'dbname')
->joinLeft(array('c' => new Zend_Db_Expr('('.$subselect.')'), 'c.pkey = a.ukey', array())
I haven't tried it but I believe it'll work.
...
->joinLeft(array('c' => new Zend_Db_Expr('(' . $subselect->assemble() . ')'), 'c.pkey = a.ukey', array())

UNION and NOT IN Mysql Operation with Zend framework

I need to execute the following mysql query in Zend;I am not an excpert with Zend framework
SELECT `optionride`.`featureoption_id`,
`optionride`.`featureoptionride_id`,`foption`.`featureoptionblock_id`,
`foption`.`labelname`,`optionride`.`value` FROM
`engine4_ride_featureoptionrides` AS `optionride`
LEFT JOIN `engine4_ride_featureoptions` AS `foption`
ON foption.featureoption_id = optionride.featureoption_id
WHERE (optionride.ride_id = '1' ) AND (foption.featureoptiontab_id= '2' )
UNION
SELECT `foption`.`featureoption_id`, null as
`featureoptionride_id`,`foption`.`featureoptionblock_id`,
`foption`.`labelname`,null as `value` FROM `engine4_ride_featureoptions` AS `foption`
WHERE (foption.featureoptiontab_id= '2' ) AND `foption`.`featureoption_id` NOT IN
(
SELECT `optionride`.`featureoptionride_id` FROM `engine4_ride_featureoptionrides`
AS `optionride`
LEFT JOIN `engine4_ride_featureoptions` AS `foption` ON
foption.featureoption_id = optionride.featureoption_id
WHERE (optionride.ride_id = '1' ) AND (foption.featureoptiontab_id= '2' )
)
Anybody can help me please.
You can put all your query asis in the $db->fetch().
Also you can use $db->select()->union(array($sql1, $sql2)), where $sql1, $sql2 can be $db->select() or a string ("select...").
For the NOT IN you can use $db->where('someField NOT IN (?)', array());