Selecting column alias filled with NULLs with Zend_Db - mysql

Is there any way you can select NULL as a column with Zend_db.
To make it clear, you could write a query:
SELECT id, name, age, NULL as ssn from dummy_table;
The result would have 4 columns id, name, age and ssn(filled with null). Is there any way to do this in Zend_Db. My guess was:
$columns = [
'id' => 'id',
'name' => 'name',
'age' => 'age',
NULL => 'ssn'
];
$select = $this->_db->select();
$select
->from('dummy_table', $columns)
But that won't work it will generate:
SELECT `dummy_table`.`id` AS `id`, `dummy_table`.`name` AS `name`, `dummy_table`.`age` AS `age`, `dummy_table`.`ssn` AS `` FROM `dummy_table`;
This query will fail. Is there any way you could do this using only Zend_Db ($this->_db->query('') is not an option)?

Have you tried using new Zend_Db_Expr('NULL') ?
You could do :
$null = new Zend_Db_Expr('NULL');
$columns = [
'id' => 'id',
'name' => 'name',
'age' => 'age',
$null => 'ssn'
];
$select = $this->_db->select();
$select
->from('dummy_table', $columns);

Related

Wordpresss - WP_query count

I need to count how many times my date recorded in the meta key:metakey_AMC_data, in format (d-m-Y) it is contained in the database by comparing it with the current date
$mostra_data_corrente = date('d-m-Y');
$query = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}postmeta
WHERE (meta_key = 'metakey_AMC_data'
AND meta_value = '$mostra_data_corrente')");
$conta_risultati = count($query);
and this I can do perfectly.but now my need is to execute the first query by linking another AND, and specify when the term slug is equal to the category of the event (terms taxonomy), obviously the query is incorrect
SELECT * FROM {$wpdb->prefix}postmeta
WHERE (meta_key = 'metakey_AMC_data'
AND meta_value = '$mostra_data_corrente')
AND(slug = 'aperitivi') "
how can i do this?
You can get that count as well. You need to modify query (code) like follow:
$qry = array(
'post_type' => 'post', // mention your post type to narrow down searching through postmeta table
'meta_query' => array(
array(
'meta_key' => 'metakey_AMC_data',
'meta_value' => $mostra_data_corrente,
'compare' => '='
)
),
'tax_query' => array(
array(
'taxonomy' => 'nameoftaxonomy', // Write the name of taxonomy that you have assinged while you created a CPT (custom post type)
'field' => 'slug',
'terms' => 'aperitivi',
)
)
)
$the_query = WP_Query($qry);
echo $the_query->post_count;
You have to make some necessary changes in above code to suite your requirements. I've added comment where you have to do changes.

CakePHP 1.3.x find('count') with 'contains', 'group', 'order' and 'limit' statement for SQL equivalent

I am currently performing a Model->query() instead of Model->find() as I can't seem to do a 'count' with 'contains', 'group', 'order' and 'limit' to represent this SQL statement.
$data = $this->Instruction->query("
SELECT `Source`.`company_name`, COUNT(*) AS `count`
FROM `instructions` AS `Instruction`
LEFT JOIN `sources` AS `Source` ON (`Instruction`.`source_id` = `Source`.`id`)
GROUP BY source_id
ORDER BY count DESC
LIMIT 5"
);
Preference would be to perform a Model->find('count') or even a Model->find('all') with the appropriate 'contains', 'group', 'order' and 'limit' but I am having no such luck.
Try this:
$options = array(
'fields'=>array('Source.company_name','COUNT(*) as `count`'),
'joins' => array('LEFT JOIN `sources` AS Source ON `Source`.`id` = `Instruction`.`id`'),
'group' => '`source_id`',
'order' => array('count' => 'DESC'),'limit' => 5
);
return $this->Instruction->find('all', $options);

ON DUPLICATE KEY UPDATE Query in Laravel4

How do I transform this
DB::table('partners')->insert(array($data));
laravel Query to have ON DUPLICATE KEY UPDATE
the structure look like
$data['program_name'] = $program['program']['_'];
$data['program_id'] = $program_id;
$data['status'] = $program['status'];
$data['shop_name'] = $shop->name;
$data['shop_logo'] = $shop->image;
$data['shop_description'] = $shop->description;
where program_id is unique
Use something like this (feel free to correct because it's untested):
DB::statement( 'INSERT INTO partners VALUES (' . implode( ',',
array_map( function( $val ) { return ":$val"; } , array_keys($data) )
) . ') ON DUPLICATE KEY UPDATE ' . implode( ',',
array_map( function( $val ) { return "$val = VALUES($val)"; } , array_keys($data) )
), $data);
I created a package that will wrapped INSERT ON DUPLICATE KEY UPDATE
https://packagist.org/packages/yadakhov/insert-on-duplicate-key
$users = [
['id' => 1, 'email' => 'user1#email.com', 'name' => 'User One'],
['id' => 2, 'email' => 'user2#email.com', 'name' => 'User Two'],
['id' => 3, 'email' => 'user3#email.com', 'name' => 'User Three'],
];
User::insertOnDuplicateKey($users);
// produces:
INSERT INTO `test_user_table`(`id`,`email`,`name`) VALUES
(1,'user1#email.com','User One'), (2,'user3#email.com','User Two'), (3,'user3email.com','User Three')
ON DUPLICATE KEY UPDATE `id` = VALUES(`id`), `email` = VALUES(`email`), `name` = VALUES(`name`)

MYSQL to CakePHP

I want to convert following complex mysql into cake's find expression:
SELECT p1, p2
FROM
(
SELECT IFNULL(a.c2, '10') AS p1, IFNULL((SELECT MAX(c.c1) FROM my_table c WHERE c.c1>p1), '30') AS p2
FROM my_table a
WHERE
(
(a.user_id = 2) AND (a.c1 BETWEEN '10' AND '30')
)
) as temp
WHERE p2 > 100
ORDER BY p1;
I tried following
http://dogmatic69.com/sql-to-cakephp-find-converter
but unable to generate the desired expression.
Please help. I really don't know how to handle such complex expressions (I do not prefer to use query in cakephp)
Thanks
Let me just convert your query into the cakephp way
if table a's model is A:
$fields = "IFNULL(A.c2, '10') AS p1, IFNULL((SELECT MAX(C.c1) FROM my_table c WHERE C.c1>p1), '30') AS p2";
$conditions = "A.user_id=2 AND A.c1 BETWEEN '10' AND '30'";
$inner_querry = $this->A->find("all", compact("fields", "conditions"));
$fields = "p1,p2";
$conditions = "p1 IN ($inner_querry) AND p2 IN($inner_query) AND p2 > 100";
$order = "p1";
$query = $this->A->find("all", compact("fields", "conditions", "order"));
debug($query); //check results of for error.
I think that sometimes it's not so wrong to use Model->query(), but let' try to use cake functions.
In fact the only way I see to obtain that particular query is building the subqueries with buildStatement() function (and at the end you still have to call Model->query() so...). But just for fun.
Assuming your model name is MyTable, in your MyTablesController do:
$p1 = "IFNULL(a.c2, '10') AS p1";
$db = $this->MyTable->getDataSource();
$subQuery = $db->buildStatement(
array(
'table' => $db->fullTableName($this->MyTable),
'alias' => 'C',
'fields' => array('MAX(c.c1)'),
'conditions' => array(
"C.c1 > " => 'p1',
)
),
$this->MyTable
);
$p2 = "IFNULL(".$subQuery.") AS p2";
$subQuery = $db->buildStatement(
array(
'table' => $db->fullTableName($this->MyTable),
'alias' => 'A',
'fields' => array($p1, $p2),
'conditions' => array(
"A.user_id" => 2,
"A.c1 BETWEEN ? AND ?" => array(10,30)
)
),
$this->MyTable
);
$query = $db->buildStatement(
array(
'table' => $subQuery,
'alias' => 'test',
'fields' => array('p1', 'p2'),
'conditions' => array(
"p2 > " => 100,
)
),
$this->MyTable
);

how to write this query using zend framework?

Basically i need to have this query done through zend framework.
SELECT k.id AS ID ,k.name AS NAME ,k.ppu_sell AS PRICE, k.type as TYPE FROM `inventory` as k UNION
select m.id AS ID, m.name AS NAME, m.price AS PRICE, 'menu' as TYPE FROM menu as m
Try this:
$select = Zend_Db_Table::getDefaultAdapter()->select();
$select->from(
array('inventory' => 'k'),
array(
'ID' => 'k.id',
'NAME' => 'k.name',
'PRICE' => 'k.ppu_sell',
'TYPE' => 'k.type'));
$selectClone = clone $select;
$select->reset()->from(
array('menu' => 'm'),
array(
'ID' => 'm.id',
'NAME' => 'm.name',
'PRICE' => 'm.price',
'TYPE' => new Zend_Db_Expr("'menu'")));
$select = Zend_Db_Table::getDefaultAdapter()->select()->union(array(
$selectClone, $select
));
Zend Framework Select Objects And UNION()