Drupal 7, query with WHERE and AND - drupal-7

I would use this query :
SELECT g.id_site, g.commune, g.latitude, g.longitude, g.altitude, g.date, g.id_fiche, a.essence, e.nom_espece, e.effectif
FROM general g, arbre a, espece e
WHERE g.nom = a.nom AND g.id_fiche = e.id_fiche AND g.id_fiche = a.id_fiche
with the drupal 7 API. I try this :
$query = db_select('general' ,'g');
$query -> join ('arbre','a','a.nom = g.nom');
$query -> join ('espece','e','e.nom = a.nom');
$query -> fields ('g', array('commune','latitude','longitude','altitude','date','id_fiche'));
$query -> fields ('a', array('essence'));
$query -> condition ('g.id_fiche','e.id_fiche','=');
$query -> condition ('g.id_fiche','a.id_fiche','=');
But I get no result :/ the problem is these two lines :
$query -> condition ('g.id_fiche','e.id_fiche','=');
$query -> condition ('g.id_fiche','a.id_fiche','=');
If I comment this two lines, I have a result (but without the WHERE clause). What should I do for properly use the WHERE clause ?
thx =)

Ignoring the query issues and focusing on just the answer:
Try This:
I think you simply have to define all the fields you are actually going to use.
If that still does not work try use the Where Clause
$query = db_select('general' ,'g');
$query -> join ('arbre','a','a.nom = g.nom');
$query -> join ('espece','e','e.nom = a.nom');
$query -> fields ('e', array('id_fiche','nom'));
$query -> fields ('g', array('commune','latitude','longitude','altitude','date','id_fiche','nom'));
$query -> fields ('a', array('essence','nom'));
$query -> condition ('g.id_fiche','e.id_fiche','=');
$query -> condition ('g.id_fiche','a.id_fiche','=');

You can use db_query(), i found this method more usable.

Related

How to bind pdo key and value from Question mark?

I want to make a query select data :
$pdo = new PDO($dsn, $user, $pass, $options);
$query = "SELECT ? FROM ? WHERE ? = ?";
$pdo->prepare($query);
How can I do it?
PDOpreapare statement is not support this query right row

Query execution in codeigniter

I have a situation where I need to fetch details of an employee from the database using his ID and display them in the browser.
$sql = "SELECT * FROM employeesalarypayment WHERE empid = ".$val['empid'].";";
$query = $this->db->query($sql);
These are the statements that I have written to get the result array. My problem is how do I take a single field/column from this array? Also have I done it correctly?
Thanks in advance.
If your query return single data from database then you need to use
$row = $query->row_array()
Try this
$sql = "SELECT * FROM employeesalarypayment WHERE empid = ".$val['empid'].";";
$result = $this->db->query($sql)->result_array();
echo $result[0]['field_name'];

Drupal db_select - How to combine UNION with LIMIT properly?

I'm trying to get content from database divided by category. I want strictly max 4 entries of type "people" and three other entries of type "organization".
I tried to do it like that:
$query = db_select('node', 'n')
->fields('n', array('title','type'))
->fields('i', array('field_image_fid'))
->fields('f', array('uri'))
->condition('n.title', '%'. db_like($keys) . '%', 'LIKE')
->condition('type', array('people'))
->range(0,4);
$query->leftJoin('field_data_field_image', 'i', 'i.entity_id = n.nid');
$query->leftJoin('file_managed', 'f', 'f.fid = i.field_image_fid');
$query2 = db_select('node', 'n')
->fields('n', array('title','type'))
->fields('i', array('field_image_fid'))
->fields('f', array('uri'))
->condition('n.title', '%'. db_like($keys) . '%', 'LIKE')
->condition('type', array('organization'))
->range(0,4);
$query2->leftJoin('field_data_field_image', 'i', 'i.entity_id = n.nid');
$query2->leftJoin('file_managed', 'f', 'f.fid = i.field_image_fid');
$query->union($query2, 'UNION');
$result = $query
->execute();
The problem is that this query is returning only the first three occurrences of people or organization combined. So if there are three people returned by the query, I will not be able to see any organization.
I also tried something like this:
$query = db_query('
SELECT p.title,p.type
FROM node as p
WHERE p.type = :type
LIMIT 4'
, array(':type' => 'people',':type1' => 'organization'))->fetchAll();
$query2 = db_query('
SELECT o.title,o.type
FROM node as o
WHERE o.type = :type1
LIMIT 4'
, array(':type' => 'people',':type1' => 'organization'))->fetchAll();
$query->union($query2, 'UNION');
or like this:
$result = db_query('
SELECT title,type
FROM {node}
WHERE type = :type
LIMIT 4
UNION ALL
SELECT title,type
FROM {node}
WHERE type = :type1
LIMIT 4'
, array(':type' => 'people',':type1' => 'organization'))->fetchAll();
But these two approaches are only returning the 4 people and no organizations, I mean never..
Thank you if you can help!
Union statement in query its actually get record from first query and merge result record of first query to second query result record.
For example first case don't used limit in query of union its giving all record.
$query1 = db_select('node', 'n')
->fields('n', array('nid','title','type'))
->condition('type', array('people'));
$query2 = db_select('node', 'n')
->fields('n', array('nid','title','type'))
->condition('type', array('organization'));
$query = Database::getConnection()
->select($query1->union($query2))
->fields(NULL, array('nid','title', 'type'))
->orderBy('nid');
$result = $query->execute()->fetchAll();
From above query it get records from first query append before records of second query
As per the you example If you wanted to get 4 records from people content type and 3 record from organizations content type
used following query to solve your issue
$query1 = db_select('node', 'n')
->fields('n', array('nid','title','type'))
->condition('type', array('people'))
->range(0,4);
$query2 = db_select('node', 'n')
->fields('n', array('nid','title','type'))
->condition('type', array('organization'));
->range(0,7);
$query = Database::getConnection()
->select($query1->union($query2))
->fields(NULL, array('nid','title', 'type'))
->orderBy('nid');
$result = $query->execute()->fetchAll();

drupal 7 , multiple FROM tables, need a very sample example

Someone can give me an example where there is a multiple 'FROM' table ?
I don't understand corectly the documentation (thanks my bad english lvl -_-).
I would like use a query like :
SELECT a.one a.two b.one FROM {table1} a, {table2} b WHERE a.one = b.one
thx =)
You can use join:
http://drupal.org/node/1848348
<?php
$query = db_select('table1', 'a');
$query->join('table2', 'b', 'a.one = b.one');
$query->fields('a', array('one', 'two'));
$query->fields('b', array('one'));
$result = $query->execute();
?>
Here is asked and answered:
https://drupal.stackexchange.com/questions/27723/drupal-7-join-two-tables
More about dynamic queries
http://drupal.org/node/310075

How to add IN statement with JOIN ON clause in tablegateway ZF2

Getting all result so far until I added a 'IN' statement with join ON clause
My code is something like this:
// $this->table = 'category';
$sql = $this->getSql();
$select = $sql->select();
$select->join('user_category_subscriptions', 'user_category_subscriptions.category_id = category.id AND user_category_subscriptions.status IN (1,2,3,4)', array(), 'left');
$select->where(array('category.user_id = 2 OR (user_category_subscriptions.status IN (2,4))'));
Error
ZEND\DB\ADAPTER\EXCEPTION\INVALIDQUERYEXCEPTION
File:E:\htdocs\myproj\vendor\zendframework\zendframework\library\Zend\Db\Adapter\Driver\Pdo\Statement.php:245
Message:Statement could not be executed
Used 'echo $select->getSqlString();' to print the query:
SELECT "categories".* FROM "categories"
LEFT JOIN "user_category_subscriptions"
ON "user_category_subscriptions"."category_id" = "categories"."category_id" AND "user_category_subscriptions"."status" IN ("1""," "2""," "3""," "4")
WHERE category.user_id = 2 OR (user_category_subscriptions.status IN (2,4))
So the problem is zend's auto converting (1,2,3,4) into ("1""," "2""," "3""," "4")
Any idea to solve this? Thanks
I think you can pass an expression instead of the join string:
// $this->table = 'category';
$sql = $this->getSql();
$select = $sql->select();
$join = new Expression('user_category_subscriptions.category_id = category.id AND user_category_subscriptions.status IN (1,2,3,4)');
$select->join('user_category_subscriptions', $join, array(), 'left');
$select->where(array('category.user_id = 2 OR (user_category_subscriptions.status IN (2,4))'));
try this
$select->where
->AND->NEST->equalTo('category.user_id', 2)
->addPredicate(new Sql\Predicate\In('user_category_subscriptions.status', array(2,4)));

Resources