mysql - get number of occurrences from "different" query results - mysql

I was wondering how could I get the number of occurrences of a common string from different results (or using OR in my query, as example below).
table example:
id | name | rank
1 | name1 | 1
2 | name1 | 1
3 | name2 | 1
4 | name3 | 1
5 | name1 | 2
6 | name1 | 2
7 | name3 | 2
Now, I need to count number of occurrences for rank = 1 and rank = 2, without duplicating the count.
doing something like this:
SELECT name, COUNT(DISTINCT name)
AS name_num FROM table WHERE rank = 1 GROUP BY name;
results is
name1 | 1
name2 | 1
name3 | 1
perfect, but now I need to include some other result (i.e. rank = 1 OR rank = 2) and get the occurrences from each name, without duplicating it.
the wanted result for query example using table example and rank = 1 OR rank = 2 should be:
name1 | 2
name2 | 1
name3 | 2
I'll try to explain the result I want:
name1 is present when rank = 1 (+1) and when rank=2 (+1);
name 2 is only present when rank=1
name3 is present when rank = 1 (+1) and when rank=2 (+1);
Is it possible?

Select Name,
Count(Distinct Rank) as Ranks
from TableName
where Rank=1 or Rank=2
Group By Name
Sql Fiddle Demo

You want COUNT(DISTINCT rank), not COUNT(DISTINCT name). Since you're grouping by name, there will only be one distinct name in each group.
SELECT name, COUNT(DISTINCT rank) name_num
FROM table
WHERE rank in (1, 2)
GROUP BY name

Select Name,
Count(distinct Rank) as Ranks
from TableName
where Rank=1 or Rank=2
Group By Name
This is what you asked for?

Related

SQL How to select columnA and columnB where columnB has a distinct value

I have a table called emp
id| name
1 | jon
1 | trav
2 | jon
2 | jon
3 | jon
3 | jon
I would like to select only the distinct ids that have the value "jon" in the name column.
I do not want the ids where there is one value "jon" and another value as something else within the name column.
The output should look like this:
id|name
2 |jon
3 |jon
You may use the following
The logic is to find out the total count of names and the count of names by id where name='jon' and if it matches return the output.
Here is one way to do this.
--Using max since we need an aggregate function when grouping by id, to bring any other columns.
create table t(id int, name varchar(50))
insert into t
select 1,'jon' union all
select 1,'trav' union all
select 2,'jon' union all
select 2,'jon' union all
select 3,'jon' union all
select 3,'jon'
select id,max(name)
from t
group by id
having count(distinct name)=count(distinct case when name='jon' then 1 end)
+----+-----------+
| id | max(name) |
+----+-----------+
| 2 | jon |
| 3 | jon |
+----+-----------+
Db Fiddle link
https://dbfiddle.uk/?rdbms=mysql_5.6&fiddle=637432cca3238285cb888889e1fa6ec7
SELECT id, name
FROM emp
WHERE name = 'jon'
GROUP BY id, name
HAVING COUNT(*) > 1
A NOT EXISTS clause will work well here, and perform well too.
SELECT id, name
FROM tbl t
WHERE name = 'jon'
AND NOT EXISTS (SELECT 1 FROM tbl a WHERE a.id = t.id AND a.name != t.name)
I would use:
select id, min(name) as name
from emp
group by id
having min(name) = max(name) and min(name) = 'Jon';

How to filter with sql condtions in one query?

Hard to say how is my problem, but What I want to to is here:
SELECT name
, (SELECT count(age) as count)
FROM 'students'
WHERE class = '1c'
;
SELECT name
, (SELECT count(age) as count)
FROM 'students'
WHERE class = '2b'
They are 2 separate mysql sentence, So how do I do that count in one sentence
to show something like:
+--------+-------+
| name | count |
+--------+-------+
| Peeter | 5 |
| Harry | 3 |
+--------+-------+
You can try below - use group by and in operator
SELECT name, count(age) as count
FROM 'students'
WHERE class in ('1c','2b')
group by name

Limit MySQL Results to One From Each "Group"

Suppose we have a table like the one below.
Id | Name | Group
-----------------
1 | John | 1
2 | Zayn | 2
3 | Four | 2
4 | Ben_ | 3
5 | Joe_ | 2
6 | Anna | 1
The query below will select all of them.
SELECT `Name` FROM `Table` WHERE 1;
How would I select only one person from each group? Who it is doesn't really matter, as long as there's only one name from group 1 and one name from group 2 etc.
The GROUP BY clause isn't fit for this (according to my error console) because I am selecting non aggregated values, which makes sense.
The DISTINCT clause isn't great here either, since I don't want to select the "Group" and definitely not group by their names.
If is not important the resulting name You can anawy leverage some group functions eg with max or min..
leverage the group functions
select max(name) from your_table
group by Group;
otherwise you can use subquery
select name from your_table
where Id in (select min(Id) from your_table group by Group);

Mysql search using where clause in same column

Hello I'm having a problem.
I want to search values in the name column using the where clause. Example table is this with values stored.
table1
--------------------------
id | name | anotherid
--------------------------
1 | name1 | 1
2 | name2 | 1
2 | name2 | 2
I have textboxes that I can change how many they appear. For example I have two textboxes. The value on textbox one is 'name1' and the second is 'name2'
My query must find those two values in table1 and, when they're found, their anotherid column must be the same also.
From the first table, All I want to get is this:
table1
--------------------------
id | name | anotherid
--------------------------
1 | name1 | 1
2 | name2 | 1
However when a situation like this appears and I still have two textboxes with the same values:
table1
--------------------------
id | name | anotherid
--------------------------
1 | name1 | 1
2 | name2 | 1
2 | name2 | 2
3 | name3 | 1
The result must be empty.
Now back to the first table. I used this code.
SELECT * FROM `table1` WHERE name = "name1" and name = "name2"
and
SELECT * FROM `table1` WHERE name = "name1" and name = "name2" group by anotherid
but all I get are empty results.
How do you do this?
The problem is in you WHERE clause,
WHERE name = "name1" and name = "name2" will always return false. Use OR instead.
Try any of these:
1. Selecting with name1,name2 and anotherid=1
SELECT *
FROM table1
WHERE (name="name1" OR name="name2")
AND anotherid=1
The result will be:
id name anotherid
1 name1 1
2 name2 1
2. Grouping by anotherid:
SELECT * FROM table1
GROUP BY anotherid
Result:
ID NAME ANOTHERID
1 name1 1
2 name2 2
3. Grouping by name:
SELECT * FROM table1
GROUP BY name
Result:
ID NAME ANOTHERID
1 name1 1
2 name2 1
SELECT * FROM `table1` WHERE name = "name1" OR name = "name2" group by anotherid
That should do.
Try this
SELECT * FROM `table1` group by name

MySQL query help wanted

I'm relatively new to MySQL, and it's possible that what I want may not be possible to achieve with one single query. I have the following table structure:
name | value1 | value2
I have several records where the names are the same but different values. What I would like to do is to select distinct names, but their values should be added. Here's an example:
john | 1 | 2
jane | 6 | 3
mark | 2 | 5
mark | 3 | 1
So the query (if possible) would return
john | 1 | 2
jane | 6 | 3
mark | 5 | 6
Yes, you can using group by.
Like
select name , sum(value1) as sum1, sum(value2) as sum2 from mytable group by name
You use group by with agregating functions.
SELECT name, sum(value1) as total1,sum(value2) as total2 FROM mytable GROUP BY name
SELECT name, sum(value1) as column1 ,sum(value2) as column2
FROM mytable
GROUP BY name
I recommend to learn a few lessons on MySQL.
Me as a beginner it was very helpful.
$query = "SELECT name, SUM(value1) as v1, SUM(value2) as v2 FROM table GROUP BY name";