简体   繁体   中英

Multiple column in IN() clause with zend framework

Is it possible to do this sort of query in zend frameowrk?

SELECT * 
FROM `relations` 
WHERE (root_type, root_id) IN ( ("PRJ", 12), ("PRJ", 13), ("GRP", 42))

I only found a way to make a query with a IN clause on one column but not on two columns.

You can JOIN them instead, like this:

SELECT r.*
FROM relations r
INNER JOIN
(
   SELECT 'PRJ' AS root_typ, 12 AS root_id
   UNION ALL 
   SELECT 'PRJ',             13
   UNION ALL 
   SELECT 'GRP',             42
) AS t  ON r.root_type = t.root_type
       AND r.root_id   = t.root_id;

This is a ZF1 query using Zend_Db_Statement :

//common way to aquire currently selected db adapter
$db = Zend_Db_Table::getDefaultAdapter();
//$db is the currently selected database adapter.
$stmt = $db->query(
            SELECT * FROM `relations` 
            WHERE (root_type, root_id) 
            IN ( ("PRJ", 12), ("PRJ", 13), ("GRP", 42))
            );

This answer is not intended to be snide. For complex database queries, Zend_Db_Statement is often the easiest/simplest/best way to perform the query.

If you would like an explanation more tailored to your needs, please provide more info on your structure. In Zend Framework (1 or 2) there are often many ways to accomplish any given task.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM