How could I find the most common value in table player_frags of either lasthit or mostdamage and order by asc?
SELECT DISTINCT(name) FROM players p
INNER JOIN player_frags pf ON pf.lasthit = p.name
OR pf.mostdamage = p.name
SELECT name FROM players p
INNER JOIN player_frags pf ON pf.lasthit = p.name
OR pf.mostdamage = p.name GROUP BY name Order By COUNT(*) DESC
You could add LIMIT 1 at the end for the most common name.
SQL Fiddle
I don't think you tried, this looks exactly like the other SQL you posted in the other question you sent
Anyway this will return name vs frequency of appearing:
SELECT COUNT(*) AS Freq, name
FROM players
GROUP BY players.name
ORDER BY COUNT(*)
Related
SELECT Count(Teams.Name)
, Teams.TeamID
, Teams.Name
FROM Teams, Rosters
WHERE Rosters.TeamID = Teams.TeamID
Is my query so far. I'd like it to print the number of entries of Rosters.TeamID that correspond to Teams.TeamID.
I think the query you want is:
SELECT t.TeamID, t.Name, COUNT(*)
FROM Teams t JOIN
Rosters r
ON r.TeamID = t.TeamID
GROUP BY t.TeamID, t.Name;
Basically, your query is missing the GROUP BY, but you should also learn proper JOIN syntax.
I have a table with real estate agent's info and want to pull firstname, fullname, and email from rets_agents.
I want to then get a count of all of their sales from a different table called rets_property_res_mstr.
I created a query that doesn't work yet so I need some help.
SELECT r.firstname, r.fullname, r.email
from rets_agents r
LEFT JOIN rets_property_res_mstr
ON r.email = rets_property_res_mstr.ListAgentEmail
LIMIT 10;
I'm not sure how to get the count in this.
You seem to be looking for aggregation:
SELECT a.firstname, a.fullname, a.email, COUNT(p.ListAgentEmail) cnt
FROM rets_agents a
LEFT JOIN rets_property_res_mstr p ON r.email = p.ListAgentEmail
GROUP BY a.firstname, a.fullname, a.email
ORDER BY ?
LIMIT 10;
Note that, for a LIMIT clause to really make sense, you need a ORDER BY clause so you get a deterministic results (otherwise, it is undefined which records will be shown) - I added that to your query with a question mark that you should replace with the relevant column(s).
I would consider using a CTE for this:
WITH sales as (
SELECT ListAgentEmail, count(*) count_of_sales
FROM rets_property_res_mstr
GROUP BY ListAgentEmail
)
SELECT r.firstname, r.fullname, r.email, count_of_sales
from rets_agents r
LEFT JOIN sales
ON r.email = sales.ListAgentEmail
LIMIT 10;
I am answering questions about an IMDB database as shown below.
I need to find which TV show (which is a kind_type that shows up as 'tv series') has the most episodes, actors and actresses, and seasons (these are separate parts of the question).
To start off, I wrote a query to find the name of the TV show that has the most actresses:
SELECT *
FROM (
SELECT DISTINCT t.title, count(t.title) total
FROM title t
INNER JOIN kind_type k
ON (t.kind_id = k.id)
INNER JOIN cast_info c
ON (c.movie_id = t.id)
CROSS JOIN role_type r
GROUP BY t.title
HAVING r.role = 'actress' AND k.kind = 'tv series'
ORDER BY total DESC
) as newTable
LIMIT 1
However, I get the error:
column "r.role" must appear in the GROUP
BY clause or be used in an aggregate function
LINE 11: HAVING r.role = 'actress' AND k.kind = 'tv series'
So you can think of it as having a lot of cast_info objects, each attached to role_type objects. Each cast_info also has a variable for the movie_id, and I aimed to select a list of all cast_info objects that had role_types with the role 'actress', and then pick out the most frequently occurring 'movie_id' that shows up in that list.
Example:
In this example, the query should ideally return "3" because that is the movie ID that has the most actresses.
Any tips would be greatly appreciated.
This is a simple fix and likely just a mistake on your part.
You're receiving the error because you're putting a regular condition inside your HAVING clause. HAVING is used for conditions regarding aggregate functions.
For example, if you were trying to select only rows with a total greater than 2, you use having:
HAVING total > 2
However, what you want needs to go in a WHERE clause. Try this:
SELECT *
FROM (
SELECT DISTINCT t.title, count(t.title) total
FROM title t
INNER JOIN kind_type k
ON (t.kind_id = k.id)
INNER JOIN cast_info c
ON (c.movie_id = t.id)
JOIN role_type r
ON (r.id = c.role_id)
WHERE r.role = 'actress' AND k.kind = 'tv series'
GROUP BY t.title
ORDER BY total DESC
) as newTable
LIMIT 1
Here is more info on the HAVING clause.
My tables are:
Parent_Child (Parent_SSN, Child_SSN)
Person (SSN, Name, age, sex)
School (Child_SSN, School_Name)
I want to select the parents(female,male) who have atleast one of their children in a particular school 'X'.I have a working query and my mysql query is:
select group_concat(p.name) from person p,parentchild pc,school s
where s.schoolname='X' and s.childssn=pc.childssn and p.ssn=pc.parentssn
group by pc.childssn
This displays the result as parent(male,female) but I want the result in (female,male) form and if I group it by parent.sex it displays results in individual rows and not in a single row.I am out of ideas.
Sample desired output:
name
Angela,Jim
Output of my above existing query:
name
Jim,Angela
Here's a Sql Fiddle for you.
SELECT DISTINCT group_concat(p.name ORDER BY p.sex Asc)
FROM Person p JOIN Parent_Child pc ON p.ssn=pc.Parent_SSN
JOIN School s ON s.Child_SSN = pc.Child_SSN
WHERE s.School_Name='X'
GROUP By pc.Child_SSN;
Try this, might possible using sort
SELECT GROUP_CONCAT( p.name ORDER BY p.name DESC ) from person p,parentchild pc,school s
where s.schoolname='X' and s.childssn=pc.childssn and p.ssn=pc.parentssn
group by pc.childssn
You can use ORDER BY inside the GROUP_CONCAT function in this way :
Try this
SELECT group_concat(DISTINCT p.name ORDER BY p.name Asc)
FROM person p JOIN parentchild pc ON p.ssn=pc.parentssn
JOIN school s ON s.childssn = pc.childssn
WHERE s.schoolname='X'
GROUP By pc.childssn
See http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function%5Fgroup-concat
I somehow managed to get the results using join here is my working query.
select * from (select distinct p.name from person p,parentchild pc,school s where s.schoolname='X' and s.childssn=pc.childssn and p.ssn=pc.parentssn and p.sex='F')q1 join (select distinct p.name from person p,parentchild pc,school s where s.schoolname='X' and s.childssn=pc.childssn and p.ssn=pc.parentssn and p.sex='M')q
I want to still know whether it is possible to sort items by a particular condition in group_concat result set which will affect it's postion in the group_concat result set.
How can I use MySQL to count with a LEFT JOIN?
I have two tables, sometimes the Ratings table does not have ratings for a photo so I thought LEFT JOIN is needed but I also have a COUNT statement..
Photos
id name src
1 car bmw.jpg
2 bike baracuda.jpg
Loves (picid is foreign key with photos id)
id picid ratersip
4 1 81.0.0.0
6 1 84.0.0.0
7 2 81.0.0.0
Here the user can only rate one image with their IP.
I want to combine the two tables in order of the highest rating. New table
Combined
id name src picid
1 car bmw.jpg 1
2 bike baracuda.jpg 2
(bmw is highest rated)
My MySQL code:
SELECT * FROM photos
LEFT JOIN ON photos.id=loves.picid
ORDER BY COUNT (picid);
My PHP Code: (UPDATED AND ADDED - Working Example...)
$sqlcount = "SELECT p . *
FROM `pics` p
LEFT JOIN (
SELECT `loves`.`picid`, count( 1 ) AS piccount
FROM `loves`
GROUP BY `loves`.`picid`
)l ON p.`id` = l.`picid`
ORDER BY coalesce( l.piccount, 0 ) DESC";
$pics = mysql_query($sqlcount);
MySQL allows you to group by just the id column:
select
p.*
from
photos p
left join loves l on
p.id = l.picid
group by
p.id
order by
count(l.picid)
That being said, I know MySQL is really bad at group by, so you can try putting the loves count in a subquery in your join to optimize it:
select
p.*
from
photos p
left join (select picid, count(1) as piccount from loves group by picid) l on
p.id = l.picid
order by
coalesce(l.piccount, 0)
I don't have a MySQL instance to test out which is faster, so test them both.
You need to use subqueries:
SELECT id, name, src FROM (
SELECT photos.id, photos.name, photos.src, count(*) as the_count
FROM photos
LEFT JOIN ON photos.id=loves.picid
GROUP BY photos.id
) t
ORDER BY the_count
select
p.ID,
p.name,
p.src,
PreSum.LoveCount
from
Photos p
left join ( select L.picid,
count(*) as LoveCount
from
Loves L
group by
L.PicID ) PreSum
on p.id = PreSum.PicID
order by
PreSum.LoveCount DESC
I believe you just need to join the data and do a count(*) in your select. Make sure you specify which table you want to use for ambigous columns. Also, don't forget to use a group by function when you do a count(*). Here is an example query that I run on MS SQL.
Select CmsAgentInfo.LOGID, LOGNAME, hCmsAgent.SOURCEID, count(*) as COUNT from hCmsAgent
LEFT JOIN CmsAgentInfo on hCmsAgent.logid=CmsAgentInfo.logid
where SPLIT = '990'
GROUP BY CmsAgentInfo.LOGID, LOGNAME, hCmsAgent.SOURCEID
The example results form this will be something like this.
77615 SMITH, JANE 1 36
29422 DOE, JOHN 1 648
Hope that helps. Good Luck.