I have a table like :
a.id; a.username; a.mail; a.optin
Considering theses values :
1; tobby; tobby#google.com; true
2; franck; tobby#google.com; true
3; john; john#google.com; true
I would like (in SQL, but also in Symfony with Doctrine) to get ALL values and ALL rows (id, username, mail, optin) BUT Distinct by email.
With SELECT DISTINCT mail, I only have mails....
Could you help me please ? Thanks !
$qb = $em->getRepository('Your Entity here')->createQueryBuilder('a')->groupBy('a.mail');
$result = $qb->getQuery()->getResult();
Here's more explanation:
The first line builds your query with a select all columns of the entity you fill in and the groupBy will give you distinct rows by email.
In MySql the query generated from this is equivalent to:
SELECT *
FROM 'Your Entity here'
GROUP BY mail
Related
I am trying to use a postgreSQL INSERT query with a subquery as parameter value. This is to find the corresponding user_id from an accompanying auth_token in user_info tabel first and then create a new entry in a different table with the corresponding user_id.
My query looks something like this
INSERT INTO user_movies(user_id, date, time, movie, rating)
VALUES ((SELECT user_id FROM user_info where auth_token = $1),$2,$3,$4,$5)
RETURNING *
I know that a query such as this will work with a single value
INSERT INTO user_movies(user_id)
SELECT user_id FROM user_info where auth_token = $1
RETURNING *
but how do I allow for multiples input values. Is this even possible in postgreSQL.
I am also using nodejs to run this query -> therefore the $ as placeholders.
To expand on my comment (it is probably a solution, IIUC): Easiest in this case would be to make the inner query return all the values. So, assuming columns from the inner query have the right names, you could just
INSERT INTO user_movies(user_id, date, time, movie, rating)
SELECT user_id,$2,$3,$4,$5 FROM user_info where auth_token = $1
RETURNING *
Note this form is also without VALUES, it uses a query instead.
Edited 20220424: a_horse_with_no_name removed the useless brackets around SELECT ... that appeared in my original version; thanks!
YOu could try uising where IN clause
INSERT INTO user_movies(user_id)
SELECT user_id
FROM user_info
WHERE auth_token IN ($1,$2,$3,$4,$5)
RETURNING *
I'm building a SQL query (Postgres is it matters), that will return me the list of articles with all the fields and with total number of references.
$a = Articles::select(DB::raw('
*,
count(
select * from "references"
where exists (select * from "users" where "users"."reference_id" = "references"."id"
and "article_id" = ?????
) as total'
))->where('created_at', '<', $date)->get();
I simplified it a little bit; there more 'exists' conditions inside the count(); there is also more ->where() rules that are dynamic and hard to rewrite in raw SQL. My main misunderstanding is how to put the corresponding article_id instead of ?????. Could someone give me a hint.
Try this its about binding parameter to Raw query.
https://laracasts.com/discuss/channels/laravel/how-to-bind-parameters-to-a-raw-query?page=1
You may pass in the parameters to be bound to the raw select using a PHP array:
$a = Articles::select(DB::raw('
*,
count(
select * from references r
where exists (select 1 from users u
where u.reference_id = r.id and article_id = ?)
) as total', ['some id here']))
->where('created_at', '<', $date)
->get();
There may be a better way to write your query in Postgres. If you can add some sample data, maybe more can be said about that.
I currently have the table as follows on the picture.
I would like to write a query which returns all the names and the travel_date with the maximal 'total' value for each name. For example, I would like the query to return in this case:
Armand 2012-07-18 and Elish 2012-06-18. How could I do that ? Thanks in advance
In most cases, you'll find that the procedure for this is relatively universal. The following example will work in MySQL, MSSQL and DB2 (among others).
SELECT
a.name,
a.travel_date,
a.total
FROM test_table AS a
INNER JOIN ( SELECT `name`, MAX(total) AS `total` FROM test_table GROUP BY `name` ) AS b
ON a.name = b.name
AND a.total = b.total;
Here's an example of the sample data I worked with including the results after running the query.
-
Edit: As jarlh, the initial query I wrote was indeed wrong. The following query should provide the results that you requested in the comment below.
SELECT name, MAX(travel_date)
FROM my_table
GROUP BY name;
This is the query I need to convert from SQL to CodeIgniter:
select *, count(username) as totalattempt from (select * from my_table order by created desc) as created2 group by `username` order by created desc
I've tried searching on Active Record CodeIgniter, but failed to get an answer.
Thanks.
Codeigniter didn’t provided any option to use sub queries with active record class.
Try:
$this->db->select('*')->from('my_table')->order_by('created','DESC');
$subQuery = $this->db->get_compiled_select();
/* Main Query */
$result = $this->db->select('created2.*, count(created2.username) as totalattempt')
->from('('.$subQuery.') as created2')
->group_by('created2.created','DESC')
->order_by('created2.username')
->get()
->result_array();
Thanks!
I have the following PgSQL query, in which i'm looking where user_id equals any one of a set of id's i am looking for.
Question is, is there a way to simply the statement so that I dont have to keep putting user_id= ?
SELECT *
FROM comments
WHERE session_id=1
AND (user_id=10 OR user_id=11
OR user_id=12 OR user_id=13
OR user_id=14 OR user_id=15
OR user_id=16)
SELECT *
FROM comments
WHERE session_id=1
AND user_id in (10,11,12,13,14,15,16);
alternatively for this specific case:
SELECT *
FROM comments
WHERE session_id=1
AND user_id between 10 and 16;