I have a table called story_comment with (integer) columns story_id, and comment_id. I want to know how many comments each story has but I'm not sure the best way to write the sql query.
The query should return a table with the columns story_id and num_comments (where num_comments is the number of rows in story_comment where story_id is the same as the story_id in that results row).
Desired Results (Example):
story_id | num_comments
4 | 17
6 | 0
7 | 4
I was able to do this for one particular story_id with the query:
SELECT story_id, Count(story_id) as num_comments FROM story_comment where story_id=20;
but I'm not sure how I can do this for every story_id in the table. Side note I'm going to be doing this query using mysqli in php.
Use GROUP BY
SELECT story_id, Count(story_id) as num_comments FROM story_comment
GROUP BY story_id
The GROUP BY statement is used in conjunction with the aggregate
functions to group the result-set by one or more columns.
To make aggregate functions like count() apply to each unique value of a column instead to the complete table, add a group by
SELECT story_id, Count(*) as num_comments
FROM story_comment
group by story_id
Related
I need one help.I need query to skip duplicate column value according to the auto increment id using MySQL.I am explaining my table structure below.
db_subcat:
member_id day_id
16 2
18 2
18 2
18 3
Here i need to skip the duplicate column value according to the member_id e.g- here for member_id-18 there are two day_id as 2,i need to fetch only one if member_id is same.
you can use distinct:
select distinct member_id, day_id from db_subcat;
you can use distinct as well as group by
select distinct member_id, day_id from db_subcat;
select member_id, day_id from db_subcat group by member_id, day_id;
Here distinct will be faster than group by. To see the difference have a look at http://charlesnagy.info/it/mysql/difference-between-distinct-and-group-by
You can use the UNIQUE key in MySQL so that the duplicate results will not be inserted in the first place.
Else, you can use SELECT distinct:
SELECT distinct member_id, day_id from db_subcat;
More information on SQL Unique Constraint: http://www.w3schools.com/sql/sql_unique.asp.
More information on SQL Select Distinct: http://www.w3schools.com/sql/sql_distinct.asp.
Apart from skipping the duplicate member id, you may want the sum of day_id as well, below query fetches that:
select member_id, sum(day_id)
from db_subcat
group by member_id
You can use any aggregate function (e.g. min, max, count, avg) according to the requirement.
I have two MySql Tables as follows:
resource
-----------------------------------------------
id name group owner_id
-----------------------------------------------
1 MyResource1 hs 11
2 MyResource2 ms 24
3 MyResource3 ps 11
...
resource_access
-----------------------------------------------
id resource_id user_id
-----------------------------------------------
1 1 12
2 2 24
3 2 11
4 3 15
...
Now, the first table is a list of resources, of course, and their respective owners in the owner_id column. The second table is the result of "sharing" this resource with another user. The table resource_access may contain records with a user_id that is equivalent to the owner_id in a row of the resource_access as a result of messy cleanup from an owner exchange.
I simply want to get the id, name, and group of any resource that a user has access to, whether they are the owner or it has been shared with them. Here is my MySQL query for an example user (24):
SELECT resource.id, resource.name, resource.group
FROM `resource`
INNER JOIN resource_access ON (
resource.owner_id='24'
OR (
resource_access.user_id='24' AND
resource_access.resource_id=resource.id
)
)
Right now, it returns the id, name, and group for resource number 2 multiple times (like twelve). Is there a possible cause for this? I have tried LEFT and RIGHT joins and am getting the same result. There are many records in the resource table, but none with the id of 2. There are no duplicate rows in resource_access sharing the resource with the same user twice.
Thanks in advance.
Use:
SELECT DISTINCT resource.id, resource.name, resource.group
to remove duplicates.
The way an inner join conceptually works is that it produces a full cross-product between the two tables. This cross-product contains a row for each pair of rows in the input tables. Then it keeps the rows that match all the ON and WHERE conditions, and returns this as the result set. If there are multiple matching rows between the two tables, you'll get multiple rows in the result set.
If you were selecting columns from both tables, you would see that they're not actually the same row. They just have the same data from the resource table, but different data from the resource_access table. But you're not showing those latter columns in your result. Using DISTINCT merges all these rows in the result.
Because you are only selecting from the resource table, I would suggest putting the conditions in the where clause rather than using an explicit join:
SELECT r.id, r.name, r.group
FROM `resource` r
WHERE r.owner_id='24' or
EXISTS (select 1
from resource_access ra
where ra.resource_id = r.id and
ra.user_id = '24'
);
With this logic, the "join" cannot product duplicates.
Select the ownership of resources then union it to resources with access.
Resulting user_id column that is different from your WHERE RA.user_id value just means that resource was shared to them instead of them owning the resource. Hope this helps.
SELECT resource.name,resource.group,resource.owner_id AS user_id
FROM resource
WHERE resource.owner_id = '11'
UNION
SELECT R.name,R.group,R.owner_id AS user_id
FROM resource_access RA
LEFT JOIN resource R
ON (R.id=RA.resource_id)
WHERE RA.user_id = '11';
I have a table setup similarly as below.
genre_id series_id
1 4
1 2
2 5
4 1
2 4
3 3
What I want to do is to be able to find all series based on the mix of genres selected.
For example finding all series that have a genre id of 1 and 2. Hypothetically the result I want is the series ID of 4.
If I use
SELECT series_id FROM table WHERE genre_id = 1 AND genre_id = 2
it returns nothing.
If I use
SELECT series_id FROM table WHERE genre_id in (1, 2)
it returns everything in 1 and 2. But I just want the rows where the genre_id's intersect.
Is there any way to do this?
This should do the trick:
SELECT series_id FROM table
WHERE genre_id IN (1, 2)
GROUP BY series_id
HAVING COUNT(*) = 2
Note this is assuming that the pair (genre_id, series_id) is unique. If it is not you will have to change the HAVING clause to
HAVING COUNT(DISTINCT genre_id) = 2
Also note that the number 2 in the HAVING clause must match the amount of items in the IN clause.
You can think of the IN() predicate as basically a series of OR terms; it's equivalent to
SELECT series_id
FROM MyTable
WHERE genre_id = 1 OR genre_id = 2
What you want is to turn the OR into AND, but that doesn't make any sense because a WHERE expression applies to one row at a time. There's no way genre_id can be both 1 and 2 on the same row.
So you need to compare genre_id from two different rows in one expression. You can do this by joining the two rows:
SELECT t1.series_id
FROM MyTable AS t1
INNER JOIN MyTable AS t2 USING (series_id)
WHERE t1.genre_id = 1 AND t2.genre_id = 2
There's also a solution using GROUP BY as shown in another answer, but the self-join can be orders of magnitude more efficient if you define the right indexes.
I describe more details for this solution in my presentation SQL Query Patterns, Optimized.
I have two tables, one called episodes, and one called score. The episode table has the following columns:
id | number | title | description | type
The score table has the following columns:
id | userId | showId | score
The idea is that users will rate a show. Each time a user rates a show, a new row is created in the score table (or updated if it exists already). When I list the shows, I average all the scores for that show ID and display it next to the show name.
What I need to be able to do is sort the shows based on their average rating. I've looked at joining the tables, but haven't really figured it out.
Thanks
To order the results, use and ORDER BY clause. You can order by generated columns, such as the result of an aggregate function like AVG.
SELECT e.title, AVG(s.score) AS avg_score
FROM episodes AS e
LEFT JOIN scores AS s ON e.id=s.showId
GROUP BY e.id
ORDER BY avg_score DESC;
You're right. You have to JOIN these tables, then use GROUP BY on the 'episodes' table's 'id' column. Then you'll be able to use AVG() function on 'the scores' tables's 'score' column.
SELECT AVG(scores.score) FROM episodes LEFT JOIN scores ON scores.showId = episodes.id GROUP BY episodes.id
SELECT episodes.*, AVG(score.score) as AverageRating FROM episodes
INNER JOIN score ON (episodes.id = score.showId)
GROUP BY episodes.id
ORDER BY AVG(score.score) DESC
I have a table with around 15 columns. What I would like to be able to do, is select a range of IDs and have all column data that is the same, presented to me.
At the minute, I have it structured as the following:
SELECT id, col_a, col_b ... count(id)
FROM table
GROUP BY col_a, col_b ...
Which returns rows grouped together that have identical data within all the rows - which is half what I want, but ideally I would like to be able to get a single row with either the value (if it's the same for every row id) or NULL if there is a single difference.
I'm not sure that it is possible, but I would rather see if it's doable in an SQL query than write some looping logic for PHP to go through and check each row's similarity.
Thanks,
Dan
UPDATE:
Just to keep this up-to-date, I worked through the problem by writing a PHP function that would find which were duplicates and then display the differences. However I have now since made a table for each column, and made the columns as references to the other tables.
E.G. In MainTable, ColA now refers to the table ColA
I'm still solving the problem with the PHP for the time being, mainly as I think it still leaves the problem mentioned above, but at least now Im not storing duplicate information.
Its a hairy thing to do, but you could do it similarly to how David Martensson suggested, I would write it like this, however:
Select a.id, a.col1, a.col2, a.col3
FROM myTable a, myTable b
WHERE a.id != b.id
and a.col1 = b.col1
and a.col2 = b.col2
and a.col3 = b.col3
That would give you the ids that are unique, but each result would have the same values for columns 1, 2, and 3. However, I agree with some of the commenters to your question that you should consider an alternative data structure, as this could better take advantage of an RDBMS model. In that case you would want to have 2 tables:
Table name: MyTableIds
Fields: id, attrId
Table name: MyTableAttrs
Fields: attrId, attr1, attr2, attr3, ect
In general, if you have data that is going to be duplicated for multiple records, you should pull it into a second table and create a relationship so that you only have to store the duplicated data 1 time and then reference it multiple times.
Make a join to a subquery with the group by:
SELECT a.id, b.col_a, b.col_b ... b.count)
FROM table a
LEFT JOIN (
SELECT id, col_a, col_b ... count(id) "count"
FROM table GROUP BY col_a, col_b ...
)b on a.id = b.id
That way the outer will select all rows.
If you still want to group answers you could use a UNION instead
SELECT id, col_a ...
WHERE id NOT IN ("SUBQUERY WITH GROUP BY")
UNION
"SUBQUERY WITH GROUP BY"
Not the nicest solution but it should work
It seems doable from how I have understood your question.
And here's a possible pattern:
SELECT
/* GROUP BY columns */
col_A,
col_B,
...
/* aggregated columns */
CASE MIN(col_M) WHEN MAX(col_M) THEN MIN(col_M) ELSE NULL END,
CASE MIN(col_N) WHEN MAX(col_N) THEN MIN(col_N) ELSE NULL END,
...
COUNT(...),
SUM(...),
WHATEVER(...),
...
FROM ...
GROUP BY col_A, col_B, ...