count with doctrine - php

I have two table:
Name
id | name | city_id
1 | aaa | 1
2 | vvv | 2
3 | ddd | 2
4 | sss | 3
5 | dds | 1
etc
City:
id | name
1 | London
2 | NY
3 | Boston
etc
how can i get City and count:
name_city | count
London | 2
NY | 2
Boston | 1
In City table:
$q = $this->createQuery('a')
->leftJoin('a.Name n')
->select('a.name_city as name_city, sum(n.city_id) as sum');
return $q->execute();
but this is wrong.

You should use count() instead of sum(), and plus, you need a group by.

You do not appear to have a FROM clause, the object type is not specified for the a entity.
Also, read the aggregate values section in the documentation.

this post was kinda helpful but I though that I would add a little more details for anyone looking to join 2 tables and with an aggregate count.
e.g. this post (http://stackoverflow.com/questions/7837671/mysql-join-tables-and-count-instances) but in doctrine.
Using the example above the query would be (doctrine 2.0):
$q = $this->em->createQueryBuilder('a')
->select('a.name_city as name_city, count(n.city_id) as sum');
->from('city','a')
->leftJoin('a.Name n')
->groupBy('n.id')
->orderBy('sum')
$query = $qb->getQuery();
echo $qb->getDql(); // if you want to see the dql created
$result = $query->getResult();

Related

Laravel Eloquent GroupBy Name to Create array of ID's

I have a table in my Laravel application which I wish to query.
id | company_name | contact |
-----------------------------
1 | Tesco | Name 1 |
2 | Tesco | Name 2 |
3 | Asda | Name 3 |
4 | Tesco | Name 4 |
5 | Asda | Name 5 |
I'm trying to get an array of all unique company names with all ID numbers.
'Tesco' => [1,2,4]
'Asda' => [3,5]
I have tried
$companies = Contact::select('company_name','id')->groupBy('company_name')->get();
However this requests the 'id' to be included in the group by which defeats the purpose. I understand it's asking for this because it's not a SUM or COUNT etc.
The above table may seem unusual and I know I should have a relation to a companies table however this is necessary at this stage.
You could use GROUP_CONCAT()
$companies = Contact::select('company_name', DB::raw('GROUP_CONCAT(id) as ids'))
->groupBy('company_name')
->get();
This would return something like:
company_name | ids
Tesco | 1,2
Edit: if you want the ids in the form an array, you could just map over the collection to convert it:
$companies->map(function($column) {
$column->ids = explode(',', $column->ids);
});
That should do the trick.

MySQL Order by two different fields at same time [duplicate]

This question already has answers here:
php/mysql multiple order by
(3 answers)
How to define a custom ORDER BY order in mySQL
(4 answers)
Closed 6 years ago.
I'm trying to display items from my database, here's an example
user | 123 | abc
Jack | 0 | b
John | 0 | c
Doe | 1 | a
and how I want it displayed on site is something like this
user | 123 | abc
Doe | 1 | a
Jack | 0 | b
John | 0 | c
I want it orderying by abc(abc=1) AND 123 (cat then dog then chicken)
Tips:
abc being either 0 or 1
123 being multiple things like: chicken, dog, cat etc..
The field isn't named 123 it's just an example and this is the query i'm using right now: SELECT * FROM users order by paid=1, animal='cat', animal='dog' It's bringing paid users up first but isn't ordering by animal
drop table users;
create table users (name varchar(5),animal varchar(10),paid int);
insert into users values
('abc','dog',1),
('def','dog',0),
('ghi','chicken',0),
('jkl','cat',0);
select * from users
order by paid desc,
case
when animal = 'cat' then 1
when animal = 'dog' then 2
when animal = 'chicken' then 3
end
result
+------+---------+------+
| name | animal | paid |
+------+---------+------+
| abc | dog | 1 |
| jkl | cat | 0 |
| def | dog | 0 |
| ghi | chicken | 0 |
+------+---------+------+
This is explained in the documentation:
SELECT * FROM t1
ORDER BY key_part1,key_part2,... ;
For more complex ordering, see here:
http://dev.mysql.com/doc/refman/5.7/en/order-by-optimization.html
This should work :
ORDER BY abc, 123
You don't need to write ASC in your case since ASC is the default value.
You need to use ~ORDER BY 123 DESC and abc ASC`
replace test with your table name.
select t.user,t.123,t.abc from test t order by t.123 DESC,t.abc ASC
Demo SQL Fiddle
Output
user 123 abc
Doe 1 a
Jack 0 b
John 0 c
select a.user,a.123,a.abc from test a order by a.123 DESC,a.abc ASC

Using nested queries and many to many relationships in Doctrine's QueryBuilder

So I'm having a bit of trouble thinking of how to approach this using a query builder. Currently, I have three objects that are the following:
HelpRequest
id
...
status
Filter
id
name
statuses -> ManyToMany(targetEntity="Status")
Status
id
name
A filter can have multiple statuses so there is a table that is keeping track what statuses are part of a specific filter.
Sample Data
help_requests
---
| id | content | status |
| 1 | hello | 3 |
filters
---
| id | name |
| 1 | Active |
| 1 | Inactive |
statuses
---
| id | name |
| 1 | Open |
| 2 | Closed |
| 3 | Waiting User Response |
status_filter
---
| status_id | filter_id |
| 1 | 1 |
| 3 | 1 |
| 2 | 2 |
The status_filter table is automatically generated from a ManyToMany relationship in doctrine between a Status object and a Filter object.
Based on the given information, I've written this SQL query but now I'm having troubles writing this with QueryBuilder.
SELECT * FROM help_requests WHERE status IN (SELECT status_id FROM status_filter WHERE filter_id = 1)
If there's any more information I can give, let me know. I've read multiple questions on SO and have tried a number of things but I can't seem to get it right. I'm aware I could just hard coded that query but I'd like the experience using QueryBuilder
Thanks for the help!
Update
In the end, since I couldn't get it to work with QueryBuilder and I didn't want to create a new entity solely to map two other entities together, I decided to use createQuery() instead and this is what I came up with:
SELECT
hr
FROM
HelpRequest hr
WHERE
hr.status
IN (
SELECT
s.id
FROM
Filter f
JOIN
f.statuses s
WHERE
f.name = :name
)
Thank you everyone for the help.
Try this query, and put is in your HelpRequestsRepository class:
$subquery = $this->->select('st.status_id')
->from('/path/to/StatusFilter', 'st')
->where('st.filter_id = 1');
$query = $this->createQueryBuilder('hr')
->select('*')
->where('hr.status IN (' . $subquery->getDQL() . ')')
->getQuery();
Try this approach in the HelpRequestsRepository class:
$qb = $this->createQueryBuilder('hr');
$qb->select("hr");
$qb->join("::Status","s",Expr\Join::INNER_JOIN, "hr.status=s" );
$qb->join("::Filter","f",Expr\Join::INNER_JOIN, "s.filters=f" );
$qb->where("f.name = :name");
$qb->setParameter('name', $nameOfTheFilterToBeFound)
Hope this help

INSERT statement in joined table

I have 3 table:
tblNames:
| id | firstname | lastname |
+------+---------------+--------------+
| 1 | John | Smith |
tblJosbs (this table accepts multiple checkbox value at the same time):
| id | jobs |
+------+-----------------------+
| 1 | Nurse |
+------+-----------------------+
| 2 | Call Center Agent |
+------+-----------------------+
| 3 | Police |
tblNamesJobs (this table is used to JOIN the other 2 tables):
| id | name_id | jobs_id |
+------+-------------+-------------+
| 1 | 1 | 1 |
+------+-------------+-------------+
| 2 | 1 | 2 |
+------+-------------+-------------+
| 3 | 1 | 3 |
All is fine but can someone show me the INSERT statement for the 3rd table I should use to when I will add new information?
For example add record that John Smith is a Call Center Agent
insert into tblNamesJobs (name_id,jobs_id )
values (
select id from tblNames where
firstname='John'
and lastname='Smith' limit 1
,
select id from tblJosbs where jobs='Call Center Agent' limit 1
);
If you are already depending on tauto increment..you can get the lastinserid, depending on your adapter.
eg. mysql_insert_id
for PDO we can use --PDO::lastInsertId.
so you will have id's of earlier inserted tables, that you can save in the new one.
INSERT INTO tblNamesJobs (name_id, jobs_id) VALUES (XXXX,YYYY)
That is assuming the table's id is auto-incrementing.
It should be noted that both the name_id and jobs_id columns in the "joiner" table should be foreign keys to the respective columns in the other table.
Edit - Valex's answer goes into more detail about what to do if you don't already have the id values.
If possible, I would recommend using some sort of framework that would handle the "joiner" table for you.

How to get TOP 10 in MySQL with combined data rows?

I have a challenge I can't seem to handle.
+------+--------+-----------+-------+
| id | user | genres | books |
+------+--------+-----------+-------+
| 1 | John | crimes | 2 |
| 2 | John | scienc | 1 |
| 3 | John | nature | 4 |
| 4 | Pete | nature | 3 |
| 5 | Pete | crime | 2 |
| 6 | Mary | nature | 20 |
+------+--------+-----------+-------+
I would like to have a SQL query that gets the total amount of books the users own, no matter the genre and would like to ORDER them by who has the most.
In this example, you see that Mary has 20 books, Pete 5 and John has 7 so my desired result would be an array like:
result[0][user] = "Mary";
result[0][total] = 20;
result[1][user] = "John";
result[1][total] = 7;
result[2][user] = "Pete";
result[2][total] = 5;
How can I get this into one SQL? Should I use CONCAT or TOP or something? I use MySQL & PHP.
You need GROUP BY with SUM
SELECT `user`, SUM(books) AS total_books
FROM `table`
GROUP BY `user`
ORDER BY total_books DESC
If you only want the first 10 then you can use
SELECT `user`, SUM(books) AS total_books
FROM `table`
GROUP BY `user`
ORDER BY total_books DESC LIMIT 10`
By the way, you might want to rethink your schema slightly. Duplicating info is against the principles of normalisation. You might want to add a new owners table:
+-----------+-------------+
| owner_id | owner_name |
+-----------+-------------+
| 1 | John |
| 2 | Pete |
| 3 | Mary |
+-----------+-------------+
and then reference this by owner_id in your books table.
select user, sum(books) as total
from your_table
group by user
order by sum(books)
limit 10
SELECT sum(books) as book_count, user FROM `books` GROUP BY (user) order by book_count DESC

Categories