I am trying to join tables to prevent too many database queries, but I don't like the way the data is returning.
The Query:
SELECT person.name, dog.dog_name FROM person JOIN dog ON person.id = dog.person_id
The return looks like this:
Array
(
[0] => Array
(
[name] => Jim
[dog_name] => Wolf
)
[1] => Array
(
[name] => Jim
[dog_name] => Frisky
)
[2] => Array
(
[name] => Tina
[dog_name] => Pokedot
)
[3] => Array
(
[name] => Tina
[dog_name] => Spiky
)
)
Is it possible to have the query instead return something like:
Array
(
[0] => Array
(
[name] => Jim
[dog_name] => array(Wolf, Frisky)
)
[1] => Array
(
[name] => Tina
[dog_name] => array(Pokedot, Spiky)
)
)
The closest solution is:
SELECT person.name, GROUP_CONCAT(dog.dog_name) AS dog_names
FROM person JOIN dog ON person.id = dog.person_id
GROUP BY person.id
This returns a string which is a comma-separated list of dog names, not a PHP array. You'll have to explode() that string in application code.
Note that the default length limit for GROUP_CONCAT() is 1024 characters, and it is controlled by the configuration option group_concat_max_len.
I agree with the comment from #KonstantinWeitz, it's worthwhile to let the RDBMS do what it's best at, and then use PHP code to post-process the results into a format you want.
For example, here's how I'd do it to return the array you described:
$peoplesdogs = array();
$stmt = $pdo->query("SELECT person.name, dog.dog_name FROM person JOIN dog ON person.id = dog.person_id");
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$peoplesdogs[$row["name"]]["name"] = $row["name"];
$peoplesdogs[$row["name"]]["dog_name"][] = $row["dog_name"];
}
return array_values($peoplesdogs);
Related
So here's the thing.
I'm querying the database using Doctrine 2.5 and I'd like to use Doctrine's createQuery() method to do it. The reason is that Doctrine 2 hydrates the results in a camelCase manner, and my column names are all with underscores. And since most of the simple queries work fine with createQuery (or even with the query builder) I'd like to move all native MySQL queries to Doctrine's DQL (to get all results in camelCase so that's a standarized results). Either that or you guys can teach me how to make Doctrine 2 return the results in the same way that the columns are in the database... xD
I have the following query:
$sql = '
SELECT sp.*, so.user_id
FROM shop_order so
INNER JOIN shop_payment sp ON(so.payment_id=sp.id)
WHERE so.id = '.$orderID;
$stmt = $em->getConnection()->prepare($sql);
$stmt->execute();
$result = $stmt->fetch();
This results in the following result:
Array
(
[id] => 14
[payment_type_id] => 2
[flag] => boleto_bb
[tid] =>
[billet] => 1
[parcel_quantity] =>
[cart_name_holder] =>
[cart_date_holder] => 1999-12-01
[cart_number_holder] => 0
[datecreated] => 2017-08-30 14:51:25
[dateupdate] => 2017-08-30 16:36:18
[status] => 2
[user_id] => 16
)
But if I do it like this:
$query = $em->createQuery('
SELECT sp, so.userId
FROM ShopOrder so
INNER JOIN ShopPayment sp WITH (so.paymentId = sp.id)
WHERE so.id = ' . $orderID);
$result = $query->getArrayResult();
I get the following result:
Array
(
[0] => Array
(
[0] => Array
(
[id] => 14
[paymentTypeId] => 2
[flag] => boleto_bb
[tid] =>
[billet] => 1
[parcelQuantity] =>
[cartNameHolder] =>
[cartDateHolder] => DateTime Object
(
[date] => 1999-12-01 00:00:00.000000
)
[cartNumberHolder] => 0
[datecreated] => DateTime Object
(
[date] => 2017-08-30 14:51:25.000000
)
[dateupdate] => DateTime Object
(
[date] => 2017-08-30 16:36:18.000000
)
[status] => 2
)
[userId] => 16
)
)
How can I get the first result from the createQuery method?
I am using cakephp in one of my project. What i need is to handle complex query using single model and single array out.Since I am new to cakephp i got stucked really very bad here :
$rs = $this->User->query("
SELECT (wd.wajebaat_amt) as commited,
SUM(pd.sila_waje) as paid,
(wd.wajebaat_amt-sum(pd.sila_waje)) as balance,
FROM wajebaat_details as wd
LEFT JOIN waje_pay_details as pd ON (pd.waje_id=wd.waje_id)
WHERE wd.hof_id="123" and wd.year="2010"
GROUP BY wd.waje_id");
print_r($rs); exit();
// it displays output as
Array
(
[0] => Array
(
[wd] => Array
(
[commited] => 252000
)
[0] => Array
(
[paid] => 253829
[balance] => -1829
)
)
)
//however i need it following format
Array
(
[0] => Array
(
[wd] => Array
(
[commited] => 252000
[paid] => 200000
[balance] => 52000
)
)
)
You can use Hash (utility) method format() in cakephp 2.5 to convert the nested array into string,in previous version of cakephp the method is set(),
Hash::format(array $data, array $paths, $format)
Example :
$result = Hash::format($rs,array('{n}.wd.commited','{n}.wd.0.paid','{n}.wd.0.balance'),'%1$d,%2$d,%$d');
Output:
252000,2000000,52000
For More formating option refere cook book of cakephp
Have User and UserProfile models. User model has hasOne association with UserProfile. UserProfile mysql table is empty. When I do $this->User->find('all', array('contain' => array('UserProfile'))) instead of an empty UserProfile array, as you would expect, I get an array populated with empty fields that correspond to schema:
Array
(
[0] => Array
(
[User] => Array
(
[id] => 1
[firstname] => Joe
[surname] => Bloggs
[email] => katie.barnes#scotlandsdna.com
[password] => $2a$10$re4r7AXWQcXgkKcabUqmtO6j.7p2bA1t2SLeG93eVsiDPBgGaeXRS
[enabled] => 1
[user_group_id] => 1
[created] => 2014-06-26 15:01:38
[modified] => 2014-06-26 15:01:38
)
[UserProfile] => Array
(
[id] =>
[job_title] =>
[user_id] =>
[enabled] =>
[created] =>
[modified] =>
)
)
Anyone seen this and know how to fix it?!
This is correct behavior because the hasOne association is not optional. If you want to make it optional, then change it to a hasMany and only ever create one record.
Cake is performing a LEFT JOIN from User to UserProfile and the query result produced NULL values for the missing record. This is the same result you would get if you executed the SQL in an editor outside of Cake.
In cake php is how we can get order of query result according to 'IN' clause in the query
$array = array(8,6); // order in 'In' clause
$condition = array('Video.id' => $array);
$videos = $this->Video->find('all', array('conditions' => $conditions));
//The query will be like below
SELECT * FROM `videos` AS `Video` WHERE `Video`.`id` IN (8,6);
Currently it will give result as
Array
(
[0] => Array
(
[Video] => Array
(
[id] => 6
)
)
[1] => Array
(
[Video] => Array
(
[id] => 8
)
)
)
I need it like
Array
(
[0] => Array
(
[Video] => Array
(
[id] => 8
)
)
[1] => Array
(
[Video] => Array
(
[id] => 6
)
)
)
order Desc or asc will not retreive actual result in order. How it can retreved using cake php ?
I am using cake php, whether this can be done in mysql ?
ORDER is an option in CakePHP?
$this->Video->find('all', array('conditions' => $conditions, 'order' => array('Video.id DESC')));
In response to comment:
$this->Video->find('all', array('conditions' => $conditions, 'order' => array('FIELD(Video.id, 7, 4, 9)')));
ORDER BY FIELD("videos"."id",8,6)
i'm quote sure you can use it in cake's find
You can even use the ELT function in this way:
ORDER BY ELT(videos.id, 8,1,6,2,n,3,.....)
This works fine for me
$order = array("FIND_IN_SET(Video.id, '8,6')");
$result = $this->Video->find('all', array('conditions' => $conditions,'order' => $order);
SELECT * FROM table WHERE id IN (1,2,3,4,5)
The above query can be converted to CakePHP like so:
<?php
$ids = array(1,2,3,4,5);
$this->Model->find('all', array('conditions' => array('Model.id' => $ids)));
?>
Why not write
SELECT * FROM `videos` as `Videos` WHERE `Videos`.`id` IN (8,6) ORDER BY `Videos`.`id` DESC
It is not a best practice to use * in your SELECT statement.
Let me know if that helps.
EDIT
If needed we can also sort it using another method
Order By Case `Video`.`id`
When 8 Then 1
When 1 Then 2
When 3 Then 3
END
This query works:
SELECT Article.id,
Article.post_time,
Article.post_locked,
Article.comments_locked, Article.title,
IF(CHAR_LENGTH(Article.content)>2000,
RPAD(LEFT(Article.content,2000),2003,'.'),
Article.content) as content,
Article.tags, Category.*,
User.id, User.user_name,
Comment.comment_count
FROM `articles` as `Article`
LEFT JOIN `categories` as `Category` ON `Article`.`category_id` = `Category`.`id`
LEFT JOIN `users` as `User` ON `Article`.`user_id` = `User`.`id`
LEFT OUTER JOIN (SELECT article_id, count(*) comment_count FROM `comments`) as `Comment` ON `Article`.id = `Comment`.article_id
WHERE '1'='1'
ORDER BY `Article`.`id` DESC
But when I loop through the resultset to assign the table name along with the field using 'mysql_field_table', the 'content' returns a table name of nothing, while all others have their correct table:
Array (
[0] => Article
[1] => Article
[2] => Article
[3] => Article
[4] => Article
[5] =>
[6] => Article
[7] => Category
[8] => Category
[9] => User
[10] => User
[11] => Comment )
using
for ($i = 0; $i < $numOfFields; ++$i) {
array_push($table,mysql_field_table($this->_result, $i));
array_push($field,mysql_field_name($this->_result, $i));
}
Anyone ever try to do this? Have a solution? I want to return less data from my DB in my query. Or is it less intensive (on mysql, memory, cpu) to simply select all content and truncate the content via PHP? I thought returning less from DB would be better.
Thanks a bunch!!
Peace.
EDIT
to clear up, this is the result, you will see why it isnt what I want:
Array (
[0] => Array (
[Article] => Array (
[id] => 8
[post_time] => 1278606312
[post_locked] => 0
[comments_locked] => 0
[title] => Article 8
[tags] => test )
[] => Array (
[content] => my content for Article )
[Category] => Array (
[id] => 2
[name] => cat2 )
[User] => Array (
[id] => 3
[user_name] => user3 )
[Comment] => Array (
[comment_count] => 1 )
)
[1] => Array (
[Article] => Array (
[id] => 7
etc...
In order to use characters beyond the English alphabet and spaces in a column alias, the standard SQL means requires using double quotes (though MySQL supports using backticks IE: "`" too):
...,
IF(CHAR_LENGTH(Article.content)>2000,
RPAD(LEFT(Article.content,2000),2003,'.'),
Article.content) AS "Article.content",
...
no you cant use a as [tablename].[columnname]-like format for custom column names.
It would be weird anyway if it would work, because how can content be defined as 'Article.content' if it's not really part of the Article table dataset.
Just select the columns you need and join where needed.
But what's WHERE '1' = '1' doing in there? that will just evaluate to true as it is a boolean expression, but it won't affect your resultset.
But when I loop through the resultset
to assign the table name along with
the field using 'mysql_field_table',
the 'content' returns a table name of
nothing, while all others have their
correct table
Once you've done that magic on Article.content, to create the content field, it no longer belongs to the Article table. Rather, it belongs to the result set of that query. I believe that's the explanation for having no table associated with that field.
Imagine a GROUP BY query, with something like COUNT(*) as number. 'number' doesn't belong to any table.
If you really need the ability to know that the column had a particular source, could you have a view on top of Article which does this manipulation to content? Then the source would appear to be the view? Unfortunately, MySQL doesn't support declared computed columns in tables, that might also be useful to you in this case.
while ($row = mysql_fetch_row($this->_result)) {
$prev_table;
for ($i = 0;$i < $numOfFields; ++$i) {
if ($table[$i] == "") {
$tempResults[$prev_table][$field[$i]] = $row[$i];
}else {
$tempResults[$table[$i]][$field[$i]] = $row[$i];
}
$prev_table = $table[$i];
}
}
Oh well, mysql couldnt do what I wanted. I added the prev_table to take the one before ;)
Thanks to everyone for the help.