MySQL Combine Data + Count(*) Query FROM 2 tables - mysql

I need help with an query. I have a 'members' table and a 'comments' table.
members: userid,name,bday etc...
comments: id,userid,message,rel etc...
Untill now i used 2 queries for membersdata and commentsCount, and combined both in PHP.
My Question. Is it possible to get both (all from members && count of comments) in only one query?
This is not workung...
SELECT members.*, count(comments.*) as count
FROM members, comments
WHERE members.userid=comments.userid
group by members.userid
Does somebody know an other solution?

Here's a cleaned-up version of your query, assuming you want the userid and number of comments for each:
SELECT members.userid, count(*) as count
FROM members
INNER JOIN comments
ON members.userid = comments.userid
GROUP BY members.userid
The issues I addressed:
only selected columns that are either in the group by clause, or have an aggregate function applied to them. It is incorrect to select columns which don't satisfy either of those criteria (although MySQL allows you to do it)
replaced implicit join with explicit join, and moved join condition from where clause to on clause
replaced select ... count(comments.*) with select ... count(*). count(*) works just fine

Thank you Matt
I used your version. And im learning by the was sql-joins.
The problem with my code was the 'count(comments.*)'. Mysql does not like this!
This is working:
SELECT members.*, count(comments.rel) as count
FROM members, comments
WHERE members.userid=comments.userid
group by members.userid

Related

i want to get all column of Accounts table with this query but it is giving error

select Accounts.name, Accounts.regno Accounts.model , Accounts.slacc, count (servicing.dt) as total
from Accounts l,eft
outer join servicing on Accounts.slacc = servicing.slacc
group by Accounts.slacc,Accounts.name
The error message is
Major Error 0x80040E14, Minor Error 25515
> select Accounts.name,Accounts .model , Accounts.regno, Accounts.slacc, count (servicing.dt) as total from Accounts left outer join servicing on Accounts.slacc = servicing.slacc group by Accounts.slacc,Accounts.name
In aggregate and grouping expressions, the SELECT clause can contain only
aggregates and grouping expressions. [ Select clause = Accounts,model ]
Your query has a group by clause. If you use a group by clause in the query, then every column in the select statement has to do one of two things - either it has to be part of the group by list, or it has to be an aggregate of some kind (Sum, Count, Avg, Max, etc). If you don't do this, SQL doesn't know what to do with the column. In your case Accounts.regno and Accounts.model are listed in the select, but they are not in the group by clause and they are not aggregates - hence your error.
Assume for the moment you have two account records with the same account name and slacc, but different Regno (or model). The group by clause says they have to be joined into one record for display, but you haven't told SQL how to do that. It doesn't matter if the data isn't like that, SQL looks for possible errors first.
In this case, you probably just want all the details grouped. The simplest way is just to make sure you add all the columns needed to the group by, like this
select Accounts.name, Accounts.regno, Accounts.model, Accounts.slacc, count(servicing.dt) as total
from Accounts
left outer join servicing on Accounts.slacc = servicing.slacc
group by Accounts.slacc, Accounts.name, Accounts.regno, Accounts.model
This will fix the error, but does extra grouping you don't need, and would get very cumbersome if you had a lot more columns you wanted from account, as you'd have to add them all. Another way to handle it is to use the minimum amount of columns for the group query, then join the result of that to your main query to get the other columns. This would probably look something like this
select Accounts.name, Accounts.regno, Accounts.model, Accounts.slacc, Totals.Total
from Accounts
left outer join
( Select slacc, count(dt) as total
from servicing
group by slacc
) Totals on Totals.slacc = Accounts.slacc

SQL: Column Must Appear in the GROUP BY Clause Or Be Used in an Aggregate Function

I'm doing what I would have expected to be a fairly straightforward query on a modified version of the imdb database:
select primary_name, release_year, max(rating)
from titles natural join primary_names natural join title_ratings
group by year
having title_category = 'film' and year > 1989;
However, I'm immediately running into
"column must appear in the GROUP BY clause or be used in an aggregate function."
I've tried researching this but have gotten confusing information; some examples I've found for this problem look structurally identical to mine, where others state that you must group every single selected parameter, which defeats the whole purpose of a group as I'm only wanting to select the maximum entry per year.
What am I doing wrong with this query?
Expected result: table with 3 columns which displays the highest-rated movie of each year.
If you want the maximum entry per year, then you should do something like this:
select r.*
from ratings r
where r.rating = (select max(r2.rating) where r2.year = r.year) and
r.year > 1989;
In other words, group by is the wrong approach to writing this query.
I would also strongly encourage you to forget that natural join exists at all. It is an abomination. It uses the names of common columns for joins. It does not even use properly declared foreign key relationships. In addition, you cannot see what columns are used for the join.
While I am it, another piece of advice: qualify all column names in queries that have more than one table reference. That is, include the table alias in the column name.
If you want to display all the columns you can user window function like :
select primary_name, year, max(rating) Over (Partition by year) as rating
from titles natural
join primary_names natural join ratings
where title_type = 'film' and year > 1989;

Filter 2 MySQL Tables

have anyone here tried to filter a table using a field on a separate table? e.g. Table_1 has field of login,group,name and Table_2 has field of login,volume,profit,open_time,close_time.
since Table_2 doesn't have a group i want to filter the login by group so that the result on the query would be filtered already. i have tried the statement below which has no errors but returns no result.
"SELECT DISTINCT a.LOGIN, a.GROUP, b.LOGIN, b.SYMBOL,
SUM(b.VOLUME) NetVolume, SUM(b.PROFIT) NetProfit, b.CLOSE_TIME
FROM TABLE_1 a, TABLE_2 b
WHERE b.CLOSE_TIME BETWEEN '"+dateTimePicker1.Value.ToString("yyyy-MM-dd")+"' AND '"+ dateTimePicker1.Value.ToString("yyyy-MM-dd")+"' AND
a.GROUP NOT IN('group_a','group_b') AND
a.LOGIN = b.LOGIN
ORDER BY NetVolume";
I hope someone can help me on this.
Thanks...
You need to use INNER JOIN
So it would look something like
SELECT DISTINCT a.LOGIN, a.GROUP, b.LOGIN, b.SYMBOL,
SUM(b.VOLUME) NetVolume, SUM(b.PROFIT) NetProfit, b.CLOSE_TIME
FROM a INNER JOIN b ON a.LOGIN = b.LOGIN
WHERE b.CLOSE_TIME BETWEEN '"+dateTimePicker1.Value.ToString("yyyy-MM-dd")+"' AND '"+ dateTimePicker1.Value.ToString("yyyy-MM-dd")+"' AND a.GROUP NOT IN('group_a','group_b') AND
ORDER BY NetVolume";
For more about joins in MySql check
https://dev.mysql.com/doc/refman/5.7/en/join.html
This is too long for a comment. It doesn't address the question directly.
You have two major errors in the code:
Never use commas in the FROM clause. Always use proper explicit JOIN syntax.
Use parameters to pass values into the query.
You may need a LEFT JOIN, but you can't express this using commas.
In addition:
SELECT DISTINCT is almost never needed with GROUP BY.
The GROUP BY does not have all the unaggregated columns.
You should work on writing a clean query. This might actually fix your underlying problem. The problem is probably due to the parameter values that are passed in.

Using two SELECT statements in SQL?

I have two tables, one is 'points' which contains ID and points. The other table is 'name' and contains ID, Forename, and Surname.
I'm trying to search for the total number of points someone with the forename Anne, and surname Brown, scored.
Would I have to do a join? If so, is this correct?
SELECT Name.Forename, Name.Surname
FROM Name
FULL OUTER JOIN Points
ON Name.ID=Points.ID
ORDER BY Name.Forename;
But then I also have to add the points, so would I have to use:
SELECT SUM (`points`) FROM Points
Then there is also the WHERE statement so that it only searches for the person with this name:
WHERE `Forename`="Anne" OR `Surname`="Brown";
So how does this all come together (based on the assumption that you do something like this)?
SELECT Name.ID, Forename, Surname, SUM(Points)
FROM Name
INNER JOIN Points ON Name.ID = Points.ID
/* Optional WHERE clause:
WHERE Name.ForeName = 'Anne' AND Name.Surname='Brown'
*/
GROUP BY Name.ID, Name.Forename, Name.Surname
So, first, your answer:
select sum(points) as Points
from
Points
inner join Name on Name.ID = Points.ID
where
Name.Forename ='Anne' and Name.SurName='Brown'
Secondly, FULL JOINS are bad since they pull all values from both sets even those without matches. If you want to only return values that match your criteria (A & B) you must use an INNER JOIN.
Thirdly, here is the MySQL reference documentation on SQL statement syntax. Please consider reading up on it and familiarizing yourself at least with the basics like JOINs, aggregation (including GROUP BY and HAVING), WHERE clauses, UNIONs, some of the basic functions provided, and perhaps subqueries. Having a good base in those will get you 99% of the way through most MySQL queries.
You can write it like this with a subquery.
SELECT Name.Forename, Name.Surname, Name.ID,
(SELECT SUM (`points`) FROM Points where Points.ID = Name.ID) as total_points
FROM Name ORDER BY Name.Forename;
However, I would like to point out, that it appears that your linking of the tables is incorrect. I can not be completely sure without seeing the tables, but I imagine it should be where points.userid = name.id

Subtle difference between queries?

In the article Why Arel?, the author poses the problem:
Suppose we have a users table and a photos table and we want to select all user data and a *count* of the photos they have created.
His proposed solution (with a line break added) is
SELECT users.*, photos_aggregation.cnt
FROM users
LEFT OUTER JOIN (SELECT user_id, count(*) as cnt FROM photos GROUP BY user_id)
AS photos_aggregation
ON photos_aggregation.user_id = users.id
When I attempted to write such a query, I came up with
select users.*, if(count(photos.id) = 0, null, count(photos.id)) as cnt
from users
left join photos on photos.user_id = users.id
group by users.id
(The if() in the column list is just to get it to behave the same when a user has no photos.)
The author of the article goes on to say
Only advanced SQL programmers know how to write this (I’ve often asked this question in job interviews I’ve never once seen anybody get it right). And it shouldn’t be hard!
I don't consider myself an "advanced SQL programmer", so I assume I'm missing something subtle. What am I missing?
I believe your version would produce an error, at least in some database engines. In MSSQL your select would generate [Column Name] is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.. This is because you select can only contain values in the group by or the count.
You could modify your version to select users.id, count(photo.id) and it would work, but it would not be the same result as his query.
I would not say you have to be particularly advanced to come up with a working solution (or the specific solution he came up with) but it is necessary to do the group in a separate query either in the join or as #ron tornambe suggests.
In most DBMSs (MySQL and Postgres are exceptions) the version in your question would be invalid.
You would need to write the query which does not use the derived table as
select users.*, CASE WHEN count(photos.id) > 0 THEN count(photos.id) END as cnt
from users
left join photos on photos.user_id = users.id
group by users.id, users.name, users.email /* and so on*/
MySQL allows you to select non aggregated items that are not in the group by list but this is only safe if they are functionally dependant on the column(s) in the group by.
Whilst the group by list is more verbose without the derived table I would expect most optimisers to be able to transform one to the other anyway. Certainly in SQL Server if it sees you are grouping by the PK and some other columns it doesn't actually do group by comparisons on those other columns.
Some discussion about this MySQL behaviour vs standard SQL is in Debunking GROUP BY myths
Maybe the author of the article is wrong. Your solution works as well, and it may very well be faster.
Personally, I would drop the if alltogether. If you want to count the number of pictures, it makes sense that 'no pictures' results in 0 rather than null.
As an alternative, you can also write a correlated sub-query:
SELECT u.*, (SELECT Count(*) FROM photos p WHERE p.userid=u.id) as cnt
FROM users u