I have two columns in a MySQL table:
bname and count
bname count
---------- ----------
Blah 2
Blah 2
huh 3
lol 1
huh 3
huh 3
I want something like this. I have created the count column but I don't know how to can I show it like the above example. count column is currently empty.
You should use the good old group by...
select bname, count(*)
from mytable
group by bname
This groups the rows by the unique values, and provides the number of rows of each group.
If you want to store it in your table too:
UPDATE myTable updated
JOIN (select bname, count(*) as cnt
from mytable
group by bname) aggregate
ON updated.bname= aggregate.bname
set `count`= aggregate.cnt
note the backticks, I think you need that here
This should set all rows their respective repetition counts.
SQL Fiddle here
If you want to run a query:
select bname,
(select count(*) from t t2 where t2.bname = t.bname) as cnt
from t
This will give you the additional column on each row.
If you want to fill in the value, use update:
update t
set count = (select count(*) from t t2 where t2.bname = t.bname)
This actually changes the value in the table to the count.
Related
I am having trouble with my sql query (Select distinct didnt work).
My sql is :
select distinct
count(T2.Column1)
from Table1 t2
where T2.Column1='2017-05-210'
The actual Column 1 data only have 3 data,
But the output is 12
Nb :
- Column1 data is having 1 to Many situation with Column2,
Here are the actual data:
Column 1 Column 2
1 A
1 B
1 C
1 D
2 A
2 B
2 C
2 D
3 A
3 B
3 C
3 D
Can anyone help me?
Really appreciate for your attention.
Thanks!
You want to count distinct values, so do count(distinct ):
select count(distinct T2.Column1)
from Table1 t2
where T2.Column1='2017-05-210'
(However, your sample data and the query's data/columns do not match.)
Your sample data, result and query do not match.
However, what your query does is:
Find all records with a column1 = '2017-05-210'.
Count all of these records where column1 is not null (which is true for all these records, as column1 = '2017-05-210').
This results in one number (one row, one column). But you additionally say with DISTINCT that you want to remove any duplicates from your result rows. With one row only there can be no duplicates, so the function is superfluous here.
So think about what you want to count really. You count distinct values (i.e. count ignoring duplicates) with COUNT(DISTINCT column), but COUNT(DISTINCT column1) would return 1 of course, because you are only looking for one value which is '2017-05-210' (or zero in case there is no record matching this value).
I have table
users|visits
--------------
user1|visit1
user1|visit2
user1|visit3
user2|visit1
user2|visit2
I want to get numbered rows in any group. How i can do this?
users|visits|visit number
-----------------------
user1|visit1|1
user1|visit2|2
user1|visit3|3
user2|visit1|1
user2|visit2|2
Try as below :
SELECT
users, visits, count(*) as `visit number`
FROM tableName group by visits,users
From what you have provide about your problem i can imagine that you have records something like this
users|visits
------------
user1|visit1
user1|visit2
user1|visit1
user1|visit3
And you want to get
users|visits|visit number
-------------------------
user1|visit1|2
user1|visit2|1
user1|visit3|1
To do that use query like this
SELECT
DISTINCT a.users,
a.visits,
(select count(*) from tableName b where b.users = a.users and b.visits = a.visits) as `visit number`
FROM tableName a
Here is my query:
SELECT t.id, t.phone
FROM tablename t
It results in duplicate IDs because in one column there are two or more different values in it.
ID Phone
1 540-500-5000
1 540-888-8888
2 340-600-6000
2 340-777-7777
3 210-200-2000
4 950-600-6000
4 950-444-4444
I want to select just the first phone for each ID, in order to avoid duplicated rows just because there are two or more phones under the same ID.
Desired output:
ID Phone
1 540-500-5000
2 340-600-6000
3 210-200-2000
4 950-600-6000
SQL Fiddle:
SELECT t.id, MIN(t.phone)
FROM tablename t
GROUP BY t.id
SELECT ID, MIN(phone) MinIsTheFirst
FROM tableName
GROUP BY ID
Just having fun with the word "FIRST"
Try this:
select ID, MIN(Phone)
from tablename
group by ID
This will give you what you want if you don't care which phone is returned. If you have a way of determining the first phone, we can adjust.
Lets say I have a MySQL table that has the following entries:
1
2
3
2
5
6
7
6
6
8
When I do an "SELECT * ..." I get back all the entries. But I want to get back only these entries, that exist only once within the table. Means the rows with the values 2 (exists two times) and 6 (exists three times) have to be dropped completely out of my result.
I found a keyword DISTINCT but as far as I understood it only avoids entries are shown twice, it does not filters them completely.
I think it can be done somehow with COUNT, but all I tried was not really successful. So what is the correct SQL statement here?
Edit: to clarify that, the result I want to get back is
1
3
5
7
8
You can use COUNT() in combination with a GROUP BY and a HAVING clause like this:
SELECT yourCol
FROM yourTable
GROUP BY yourCol
HAVING COUNT(*) < 2
Example fiddle.
You want to mix GROUP BY and COUNT().
Assuming the column is called 'id' and the table is called 'table', the following statement will work:
SELECT * FROM `table` GROUP BY id HAVING COUNT(id) = 1
This will filter out duplicate results entirely (e.g. it'll take out your 2's and 6's)
Three ways. One with GROUP BY and HAVING:
SELECT columnX
FROM tableX
GROUP BY columnX
HAVING COUNT(*) = 1 ;
one with a correlated NOT EXISTS subquery:
SELECT columnX
FROM tableX AS t
WHERE NOT EXISTS
( SELECT *
FROM tableX AS t2
WHERE t2.columnX = t.columnX
AND t2.pk <> t.pk -- pk is the primary key of the table
) ;
and an improvement on the first way (if you have a primary key pk column and an index on (columnX, pk):
SELECT columnX
FROM tableX
GROUP BY columnX
HAVING MIN(pk) = MAX(pk) ;
select id from foo group by id having count(*) < 2;
This might be confusing but what I want to do is a basic select distinct columnx limit 3, the problem I have is it returns 3 rows, but I only want to get all values from one distinct columnx.
id|columnx
1 |here
2 |here
3 |Idontwant
4 |Apple
so I want a query that will return 1 and 2. The problem is columnx could be anything and I cant just say where columnx = 'here'
The limit 3 comes into play because it's hard coded into my C# app. The issue is i also set a hashset based on columnx, i have to have that columnx static for all records. However, with every query i put together there is the possibility that with my limit i'll return 2 values in columnx, which are distinct values however, i can only have distinct columnx value per query.
No what I want is all values that belong to columnx, in this example columnx ='here', when i do a distinct all I want back is the first distinct result, not 'Idontwant' or 'apple' regardless of the limit.
Here is the query and it works.
SELECT id,columnx
FROM sites where columnx= (select distinct columnx from sites limit 1) limit 3
It sounds like you want the id values for three distinct columnx values. Is this right? Try this:
SELECT id, columnx
FROM table AS t1
JOIN (
SELECT DISTINCT(columnx) AS columnx
LIMIT 1
) AS t2 ON (t1.columnx = t2.columnx);
I also wonder if you need some ORDER BY in there somewhere, but your question doesn't mention that, so I've omitted it--essentially meaning that you'll get three semi-random columnx values.
To be honest I'm not sure what you are asking either but it sounds like you want to return the ids for those rows where there are duplicate values in columnx (limit the result to 3).
SELECT id, columnx
FROM tablename
GROUP BY columnx
HAVING ( COUNT(columnx) > 1 )
LIMIT 3