Subqueries in symfony - mysql

I have a Review Entity which contains the fields:
id
userID
bookID
review
and then a Rating Entity which contains the fields:
id
reviewID
userID
rating
The rating could possible store 0 or a 1.
How would I perform a query using symfony to count all ratings from the Rating table that are equal to 1 and then count the ratings that are equal to 0 then minus the count for 0 ratings from the count for 1 ratings and return it for each review?
How would I do that? Seems a very tricky one

Alright as you are using Symfony I assume you are also use Doctrine.
Doctrine allows you to perform Native Queries. Have a look on how to execute a native query here.
On MySQL you may perform Subqueries, have a look here on how to do it.
Then try something like this:
SELECT rv.* , ((SELECT count(*) FROM ratings ra WHERE ra.rating = 1 AND id rv.id = ra.reviewID) - (SELECT count(*) FROM ratings ra WHERE ra.rating = 0 AND id rv.id = ra.reviewID)) as result FROM review rv;
Have a look here on how to get the value attached to the entity.

Related

MySQL 2 queries combine into 1 query - self referencing

We have build a very simple referral system that tracks userID's without cookies and referrals for social media. I am trying to create something like a 'leader board' so I can show the UserID of the top leaders in the database.
I'm trying to combine these 2 queries into 1 query.
SELECT
Count(users.AffiliateID) AS affiliate,
users.AffiliateID
FROM
users
group by affiliateID
order by affiliate desc
This generates an output where the variable 'affiliate' contains the USERID of the top referring affiliate. In this case the #1 person is affiliateID = 5297dc41331235
What I then do is look up the name of the person with this query.
Select name from users where UserIDHash = "5297dc41331235";
How can I rewrite the query above so that it looks up the value of the name field as a column that references the value of the AffiliateID i.e. where AffiliateID=UserIDHash on each record?
Your help is greatly appreciated.
Thanks!

ORACLE Query occurrence for an element

I'll try to explain with a generic example:
I have a table in MySQL with two columns: "Product" and "Client". For each product a client buy, a register is generated in this table.
Now I took 2 products, Stove and Refrigerator, and I want to find the client that bought both products and just them.
In this case, the expected result would be Marley, and why not Maria too? Because beyond the stove and refrigerator she also bought a chair.
How could be a MySQL ORACLE select query to give me that result?
edit: the question was wrong, it's Oracle and not MySQL, thanks Gordon Linoff
You can use aggregation. Assuming there are no duplicates in the table:
select client
from t
group by client
having sum( product in ('Stove', 'Refrigerator') ) = 2 and
count(*) = 2;
If there are duplicates, the logic is:
select client
from t
group by client
having count(distinct case when product in ('Stove', 'Refrigerator') then product end ) = 2 and
count(distinct product) = 2;
Concept Used:
Filter record for Stove and Refrigerator.
Group records by Client
Each Client should have exactly two distinct records, if yes then it should be in result set.
SELECT Client
FROM table
WHERE Product IN ('Stove', 'Refrigerator')
GROUP BY Client
HAVING COUNT(DISTINCT Product) = 2

mysql query to count number of times each Id has been used

I have a database table named badges with following structure:
UserId Name
5 reviewer
3 supporter
12 copy editor
5 master
3 master
.... and so on
here name is the name of a tag achieved by the UserId.
Now I want to count for each UserId how many number of tags he has achieved and display as output in decreasing order.
Note: the UserId and Name are not distinct, means a UserId can earn multiple tags and also multiple number of each tags.
I am having writing the mysql query for this.
This is the most basic use of COUNT with GROUP BY:
SELECT UserId, COUNT(*)
FROM badges
GROUP BY UserId
ORDER BY COUNT(*) DESC

Mysql COUNT VS num rows performance

I want to select the amount of forum posts compared to a list of users. So it will look like this:
USERID --- FORUMPOSTS
3647 - 2
7467 - 14
2673 - 39
3224 - 5
... and so on
Now I'm asking if it would be faster to count the forum post via COUNT(*) or looping through the userlist before and creating a new query for each user to count his/her forum posts via mysql_num_rows.
You can let SQL do the grouping and counting
select userid, count(*) as forumposts
from your_table
group by userid
Now I'm asking if it would be faster to count the forum post via COUNT(*) or looping through the userlist before and creating a new query for each user to count his/her forum posts via mysql_num_rows.
It'll be faster to do the former, "count the forum post via COUNT(*)". You can group the results as follows:
SELECT userid, COUNT(*) FROM my_table GROUP BY userid
It'll be even faster still if your table has an index on the userid column.

Using GROUP BY and returning all criteria matching records

I have a table that stores various types of flags. Each flag type has a reasonId column. So you could flag a post as spam and use several reasons; as abusive and use several reasons, etc.
I need a query to return all spam flags (flagTypeId=1) on a single post and, in addition, an extra column to return the number of times a flag reason occurred (reasonId). I need the full record set because I need to tack the user data, thus returning a grouped result is not sufficient by itself:
Assuming I have a flags table with PK id, int flagTypeId, int postId, int reasonId, and userId, I wrote this:
SELECT id, flagTypeId, postId, userId, reasonId, COUNT(reasonId) reasonCount
FROM flags
WHERE flagTypeId = #flagTypeId AND postId = #postId
GROUP BY reasonId
ORDER BY reasonCount DESC
This query does not return the correct number of records. If I have four spam records, and two of those four share the same reasonId, only three records come back. I want all four records to come back with an extra column showing the number of times a reasonId occurred.
Any ideas how I can modify my query to achieve this?
SAMPLE INPUT/OUTPUT
Assuming three peope flagged the same post, and two of them used the same flag reason.
id flagTypeId postid reasonid userid count
1 1 1 1 1 2
2 1 1 1 2 2
3 1 1 2 3 1
Would this work:
SELECT id, flagTypeId, postId, flags.reasonId, x.reasonCount
FROM
flags
JOIN (SELECT reasonid, COUNT(*) AS reasonCount FROM flags WHERE flagTypeId = #flagTypeId AND postId = #postId GROUP BY reasonid) AS X
ON flags.reasonid = x.reasonid
WHERE
flagTypeId = #flagTypeId AND postId = #postId
I think you're going about it a little backwards. Keep in mind that, if you're already retrieving all the information in a collection of records, you already have the count of records, just by getting the size of the returned collection.
Tweak your query to remove the GROUP BY clause and COUNT column. Then, assuming it was something like PHP, and you fetched the results of the modified query into an array $flagReasons, you can just reference count($flagReasons) to get the count.