Show all rows in MySQL that contain the same value - php

I have a MySQL database:
ID | Name
1 | Bob
2 | James
3 | Jack
4 | Bob
5 | James
How would I return a list of all the columns where the same name appears more than once, eg, I'd like to return this:
1 | Bob
2 | James
4 | Bob
5 | James
I've written a count query:
SELECT Name, COUNT(Name)
AS NumOccurrences
FROM table
GROUP BY Name
HAVING ( COUNT(Name) > 1 )
But that just returns something like this:
Bob | 2
James | 2
Whereas I want to return the full rows returned.
Any help would be greatly appreciated, thanks.

You can do it with a sub select
SELECT * FROM table WHERE Name IN (
SELECT Name FROM table GROUP BY Name HAVING count(*) > 1
)
Also if your distinction match is multiple columns, you can use cartesian sets:
SELECT * FROM table WHERE (firstName, lastName) IN (
SELECT firstName, lastName FROM table GROUP BY firstName, lastName HAVING count(*) > 1
)

Try this sql query:
select distinct a.name, a.id
from table a, table b
where a.name = b.name and a.id != b.id

Try this
SELECT Name, COUNT(Name) AS NumOccurrences
FROM table
GROUP BY Name
HAVING COUNT(*) > 0

Related

Count records from different columns (MySQL/php)

i have a table like this
id | block1 | block2 | block3
-----------------------------------------------
1 | John | John |
2 | Mark | |
3 | | Frank | Frank
how i count how many john, Mark and Frank are in the table? es.
John - 2
Mark - 1
Frank - 2
i use the query below but is only for one column and i don't know how have the same result for all the columns. I think is a helpful query becouse i read is possible have the results maybe for the last month or year
SELECT block1,
COUNT(*) AS total
FROM table
GROUP BY block1
ORDER BY total DESC;
thank you
(sorry for my english)
You can use UNION ALL to unpivot the table, then apply grouping:
SELECT block, COUNT(*) AS 'total'
FROM (
SELECT block1 AS block
FROM mytable
UNION ALL
SELECT block2
FROM mytable
UNION ALL
SELECT block3
FROM mytable) AS t
GROUP BY block
Select columns separately and use UNION to merge them. Then COUNT them. You can try this -
SELECT col_block, COUNT(col_block) FROM (
(SELECT block1 col_block,
FROM table)
UNION ALL
(SELECT block2 col_block,
FROM table)
UNION ALL
(SELECT block3 col_block,
FROM table)) tbl
GROUP BY col_block

MySQL 'Group By' messes up virtual column count

I am trying to select distinct rows within my SQL table, however I'm not having luck in labeling the returned rows appropriately using the code below:
SELECT #row:=#row+1 as rank,
a.id,
a.name
FROM table a,
( SELECT #row:=0) b
GROUP BY a.id
ORDER BY a.name ASC
This query will return the following:
| RANK | ID | NAME
--------------------------
2 | 4483 | Bob
8 | 9453 | Joe
10 | 4543 | Maurice
What I want it to return is this, however:
| RANK | ID | NAME
--------------------------
1 | 4483 | Bob
2 | 9453 | Joe
3 | 4543 | Maurice
Would it be more appropriate for me to use a DISTINCT query for a query of this magnitude?
As per Marc B's solution, I decided to wrap my query with another one however instead I decided to Select DISTINCT columns rather than grouping them which would remove my margin of error, by using this code
SELECT #row:=#row+1 as rank, a.id, a.name FROM
(
SELECT DISTINCT id, name
FROM Table1
) a, (SELECT #row:=0) b
ORDER BY a.name ASC

PHP MySQL How to get the count of rows where column is completely unique

I have a table that looks like this
id + kID
--------------------------
0 | 3
1 | 6
2 | 7
3 | 6
4 | 7
5 | 5
What I want to do is find the amount of rows where the kID occurs only once. So in this case the value of the variable should be 2 because kID: 3 and 5 occurs only once so i'm trying to count that while ignoring everything else. I am really stumped, thanks for any help.
This will show kIDs that occur only once:
SELECT kID, COUNT(kID)
FROM table
GROUP BY kID
HAVING COUNT(kID) < 2
Result
| KID | COUNT(KID) |
--------------------
| 3 | 1 |
| 5 | 1 |
See the demo
Then to get the total count of those:
SELECT Count(*) AS count
FROM (SELECT kid,
Count(kid)
FROM tbl
GROUP BY kid
HAVING Count(kid) < 2) a
Result
| COUNT |
---------
| 2 |
See the demo
Try this
SELECT
id,
count(kID) as `Count`
FROM mytable as t
GROUP BY kID
HAVING Count = 1
How about
select count(*) from
(select kid, count(*) from table group by kid having count(*) = 1)
You could do the following:
select count(*) from
(
select kID, COUNT(*) [c] from tableName
group by kID
) t
where t.c = 1
SELECT kID,
COUNT(kID)
FROM tableName
GROUP BY kID
HAVING COUNT(kID) = 1
You could do it with a sub-select. This should work, though might not be extremely efficient:
SELECT id, kID, COUNT(1) FROM (SELECT COUNT(1),kID FROM TABLE
GROUP BY kID
HAVING COUNT = 1)
One more way to do it. It will work as long as the (id) is the primary key of the table or there is a unique constraint on (kid, id):
SELECT COUNT(*) AS cnt
FROM
( SELECT NULL
FROM tableX
GROUP BY kid
HAVING MIN(id) = MAX(id)
) AS g ;
Tested at SQL-Fiddle
An index on (kid, id) will improve efficiency - and only one COUNT() will be done, not 2.

Count occurrences of distinct values in 2 fields

I am trying to find a MySQL query that will find distinct values in a particular field, count the number of occurrences of that value in 2 fields (1_user, 2_user) and then order the results by the count.
example db
+------+-----------+-----------+
| id | 1_user | 2_user |
+------+-----------+-----------+
| 1 | 2 | 1 |
| 2 | 3 | 2 |
| 3 | 8 | 7 |
| 4 | 1 | 8 |
| 5 | 2 | 8 |
| 6 | 3 | 8 |
+------+-----------+-----------+
expected result
user count
----- -----
8 4
2 3
3 2
1 2
The Query
SELECT user, count(*) AS count
FROM
(
SELECT 1_user AS USER FROM test
UNION ALL
SELECT 2_user FROM test
) AS all_users
GROUP BY user
ORDER BY count DESC
Explanation
List all the users in the first column.
SELECT 1_user AS USER FROM test
Combine them with the users from the second column.
UNION ALL
SELECT 2_user FROM test
The trick here is the UNION ALL which preserves duplicate values.
The rest is easy -- select the results you want from the subquery:
SELECT user, count(*) AS count
aggregate by user:
GROUP BY user
and prescribe the order:
ORDER BY count DESC
SELECT u, count(u) AS cnt
FROM (
SELECT 1_user AS u FROM table
UNION ALL
SELECT 2_user AS u FROM table
) subquery
GROUP BY u
ORDER by cnt DESC
Take the 2 queries:
SELECT COUNT(*) FROM table GROUP BY 1_user
SELECT COUNT(*) FROM table GROUP BY 2_user
Now combine them:
SELECT user, SUM(count) FROM
((SELECT 1_user as user FROM table)
UNION ALL
(SELECT 2_user as user FROM table))
GROUP BY user, ORDER BY count DESC;
I think this what you are looking for since your expected result did not include 7
select usr, count(usr) cnt from
(
select user_1 usr from users
union all
select user_2 usr from users
) u
where u.usr in (select user_1 from users)
group by usr
order by count(u.usr) desc

MySQL - Count distinct number of name instances in database

A column in my table contains names. I created a query:
SELECT COUNT(*) Number, (b_concat_name) Name FROM `js_b_table` GROUP by Name
that produces the following:
Number | Name
1 | Chris Smith
4 | Fred Savage
2 | Sarah McArthur
How can I update the column b_name_count in js_b_table that contains the corresponding name (b_concat_name) in that row?
If I understand correctly, you want js_b_table to look something like this:
b_concat_name | b_name_count | ... other fields ...
--------------+--------------+---------------------
fred | 3 | ... other values ...
fred | 3 | ... other values ...
fred | 3 | ... other values ...
barney | 2 | ... other values ...
barney | 2 | ... other values ...
where every record's b_name_count indicates the total number of records with the same b_concat_name. Is that correct?
If so, you can use this:
UPDATE js_b_table AS jbt1
INNER
JOIN ( SELECT jbt2.b_concat_name,
COUNT(*) AS b_name_count
FROM js_b_table AS jbt2
GROUP
BY jbt2.b_concat_name
) AS jbt3
ON jbt3.b_concat_name = jbt1.b_concat_name
SET jbt1.b_name_count = jbt3.b_name_count
;
To get a count of how many time each name is in the table it's:
SELECT
count(*) AS number,
name
FROM USERS
GROUP BY name
If I understand correctly, you want to update a column, say name_count, for each user. You can do this by executing the following query:
UPDATE USERS u
SET u.name_count =
(SELECT count(*)
FROM USERS u2
WHERE u2.name = u.name);

Categories