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
Related
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'];
I'm new in Cakephp 3.x and I'm having some trouble to create a subquery in the new ORM format. I have this report in my application, that needs to return the follow result:
1. There are three entities - Users, Calls, CallStatus.
2. Users hasMany Calls, Calls hasMany CallStatus.
3. I need to count how many CallStatus each user has in Calls.
Now follow the query that I need to put on new ORM format:
SELECT U.name,
(SELECT COUNT(*) FROM calls as C WHERE C.call_status_id =1 and C.user_id=U.id) AS 'Unavailable',
(SELECT COUNT(*) FROM calls as C WHERE C.call_status_id =2 and C.user_id=U.id) AS 'Busy',
(SELECT COUNT(*) FROM calls as C WHERE C.call_status_id =3 and C.user_id=U.id) AS 'Contacted',
(SELECT COUNT(*) FROM calls as C WHERE C.call_status_id =4 and C.user_id=U.id) AS 'Error'
FROM `users` AS U
WHERE U.profile=3 and U.is_active=1
Could someone give me a help, please? Thanks
If I understand you correctly, you want to see the number of calls for every callstatus you have for a specific user.
Try the following. Note that I used the CakePHP convention for naming the callstatuses (which is plural).
// get the tableregistry
use Cake\ORM\TableRegistry;
$callstatuses = Cake\ORM\TableRegistry::get('Callstatuses');
// for user with id 2, get the number of calls for each callstatus
$callstatuses->find()
->contain(['Calls'])
->where(['Calls.user_id' => 2, 'User.is_active' => 1])
->countBy('name')
->toArray();
// output could be:
//[ 'Unavailable' => 2, 'Busy' => 1 ]
You can find information about creating queries in the CakePHP book: see 'Query Builder'.
If you want to know more about working with/on queries, note that queries are Collections. Anything you can do on a Collection object, you can also do in a Query object. See the Collection section in the CakePHP book.
You have to use subqueries, as many as you want!
Here is an example for your case:
$q = $this->Calls->find();
$q1->select([$q->func()->count('*')])
->where(['Calls.user_id = Users.id', 'call_status_id' => 1]);
$q2->select([$q->func()->count('*')])
->where(['Calls.user_id = Users.id', 'call_status_id' => 2]);
$q3->select([$q->func()->count('*')])
->where(['Calls.user_id = Users.id', 'call_status_id' => 3]);
$q4->select([$q->func()->count('*')])
->where(['Calls.user_id = Users.id', 'call_status_id' => 4]);
$qUsers = $this->Users->find()
->select([
'id',
'first_name',
'Unavailable' => $q1,
'Busy' => $q2,
'Contacted' => $q3,
'Error' => $q4
])
->where(['profile' => 3, 'active' => 1])
->all();
Note: That nicer if you use a loop to create suqueries in this case.
can someone help me with this? I want to query some data for my joomla 2.5
template from the db.
The known mysql syntax
$result = mysql_query("SELECT * FROM `jos_fieldsattach_values` WHERE value > NOW() and value < NOW() + INTERVAL 5 DAY");
works as expexted but if I try to write this in the joomla db syntax the query fails
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$db->setQuery($query);
$query
->select(array('*'))
->from('#__fieldsattach_values');
->where('WHERE value > NOW() and value < NOW() + INTERVAL 5 DAY');
$result = $db->loadObjectList();
print_r($result);
It seems somethings wrong with the where clause ?
Thankful for you answers,
tony
First of all. When you develop anything with Joomla you should turn on FULL Error reporting (Configuration/Server/Error Reporting/Development) then set system debugging (Configuration/System/Debug System/Yes). It will show all errors, those from system and those from php/mysql. But remember make sure website is not visible for other users to. Now, your code should look more like this:
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select("*")->from('#__fieldsattach_values')->where('value > NOW() AND value < DATE_ADD(NOW(),INTERVAL 5 DAY)');
$db->setQuery($query);
$result = $db->loadObjectList();
print_r($result);
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.
Noob here.
I have a super column family sorted by timeuuidtype which has a number of entries. I'm trying to perform a simple get function with phpcassa that wont work. I'm trying to return a specific value from a UTF8 sorted column within a TimeUUID sorted SC. The exact code works with a similar SC Family sorted by BytesType.
Here is the info on the scf I'm trying to get from which i previously entered via -cli.
ColumnFamily: testSCF (Super)
Columns sorted by: org.apache.cassandra.db.marshal.TimeUUIDType/org.apache.cassandra.db.marshal.UTF8Type
RowKey: TestKey
=> (super_column=48dd0330-5bd6-11e0-adc5-343960c1b6b8,
(column=test, value=74657374, timestamp=1301603831288000))
=> (super_column=141a69b0-5c6e-11e0-bcce-343960c1b6b8,
(column=new test, value=6e657774657374, timestamp=1301669004440000))
And here is the phpcassa script I'm using to retrieve the data.
<?php
require_once('.../connection.php');
require_once('.../columnfamily.php');
$conn = new Connection('siteRoot');
$scf = 'testSCF';
$key = 'testKey';
$super = '141a69b0-5c6e-11e0-bcce-343960c1b6b8';
$col = 'new test';
$entry = new ColumnFamily($conn, $scf);
$q = ($entry->get($key, $columns=array($super)));
echo $q[$super][$col];
?>
Also if I don't specify the SC like so.
$q = ($entry->get($key));
print_r($q);
It returns:
Array ( [HÝ0[ÖàÅ49`Á¶¸] => Array ( [test] => test ) [i°\nà¼Î49`Á¶¸] => Array ( [new test] => newtest ) )
I know part of the issue might have been brought up in How do I insert a row with a TimeUUIDType column in Cassandra?
But it didn't really help me as I presumably have accepted timeuuidtypes.
Thanks for any help guys.
Suppose I didn't try hard enough to begin with. The answer in fact was everything to do with the link.
Appears that the -cli accepted what jbellis in the link describes as 32 byte representation of the timeUUID (141a69b0-5c6e-11e0-bcce-343960c1b6b8) when I inserted it. This confused me.
It works fine when you 'get()' with the "raw" 16 byte form (HÝ0[ÖàÅ49`Á¶¸).
Cheers.