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);
Related
I have the below table and I want to do the following:
Count the number of times each item appears in the table
Count the DISTINCT number of items
Group the items by name
+-------+---------+
| id | names |
+-------+---------+
| 1 | Apple |
| 2 | Orange |
| 3 | Grape |
| 4 | Apple |
| 5 | Apple |
| 6 | Orange |
| 7 | Apple |
| 8 | Grape |
+-------+---------+
For the 1. and 3. points I have the following query which works quite well:
SELECT * ,
COUNT(names) as count_name,
FROM tbl_products WHERE type = '1'
GROUP BY names
So I get:
Apple (4)
Orange (2)
Grape (2)
Now I want to also count the number of grouped by rows and added a line to count the distinct elements, however there is some problem, since MySQL accepts the query but cannot output a result:
SELECT * ,
COUNT(names) as count_name,
COUNT(DISTINCT names) as count_total
FROM tbl_products WHERE type = '1'
GROUP BY names
Can anyone advice what might be the problem?
EDIT: For more clearance I want to get a table like this:
+-------+---------+------------+-------------+
| id | names | count_ctg | count_total |
+-------+---------+------------+-------------+
| 1 | Apple | 4 | 3 |
| 2 | Orange | 2 | 3 |
| 3 | Grape | 2 | 3 |
+-------+---------+------------+-------------+
Why not just use the query you are using:
SELECT * ,
COUNT(names) as count_name,
FROM tbl_products WHERE type = '1'
GROUP BY names
This query achieves all three objectives.
1) You get a count of the number of each name value in count_name.
2) The number of distinct names values will be equal to the number of rows in the result set , since you are grouping by names. Pretty much any client-side MySQL DB connection library will enable you to retrieve this value.
3) You meet your third criteria of grouping by name by explictly using GROUP BY names
Of course the value for id in the result set is meaningless, you may want to only select names and count_names.
1-.Count the number of times each item appears in the table:
SELECT names, count(names) FROM tbl_products WHERE type = '1' group by names
2-. How many distinct items exist in the table:
SELECT DISTINCT names FROM tbl_products WHERE type = '1'
3-. Group the items by name:
SELECT count(DISTINCT names) as Total FROM tbl_products WHERE type = '1'
As your last EDIT (ALL IN ONE):
SELECT id, names, count(names), total FROM tbl_products, (select count(distinct names) as total from tbl_products) as total WHERE type = '1' group by names
You can get the count of distinct names in a subquery, then OUTER JOIN that thing back into your main query where you already solved for 1 and 3:
SELECT names ,
COUNT(names) as count_name,
Total
FROM tbl_products
OUTER JOIN (SELECT count(DISTINCT names) as Total FROM tbl_products) t2
WHERE type = '1'
GROUP BY names
You can use the SQL Windowing OVER()
This query returns the row_number() function as the id column in the results, and the over(...) for row_number requires an order by clause. You could order by whatever you want, but it most be ordered by something.
;WITH vwGroups (name, Quantity) AS
(
SELECT name
, COUNT(*)
FROM tbl_products
GROUP BY name
)
SELECT ROW_NUMBER() OVER(ORDER BY Quantity DESC, name) AS id
, name
, Quantity AS count_name
, COUNT(*) OVER () AS count_total
FROM vwGroups
Lets say i have a table in my database there looks like this:
|-------------------------|
| id | numbers |
|-------------------------|
| 1 | 1,3,5,7 |
| 2 | 2,4,6,8 |
| 3 | 1,2,3,4,5,6,7,6,8 |
|-------------------------|
I want to remove let us say 4 for all the rows that has the number 4 in the numbers column. What is the sql call to this? I'm using php and mysql.
To make it more understandable I use different table and column names. A better table design would be
users table
-------------------
id
name
other_columns
roles table
-------------
user_id
role_number
Example data:
users
--------------
id name
1 peter
2 tom
roles
----------
user_id role_number
1 1
1 3
1 7
2 2
2 8
Using this design you can now query for all roles a user has like this
select r.role_number
from users u
join roles r on u.id = r.user_id
where u.name = 'peter'
or if you already have the users ID then
select role_number
from roles
where user_id = 1
Using string functions you may try to write following query:
UPDATE
`table_name`
SET
`numbers` = TRIM(BOTH ',' FROM REPLACE(CONCAT(',', `numbers`), ',4', ''))
WHERE
FIND_IN_SET('4', `numbers`);
Test:
SELECT TRIM(BOTH ',' FROM REPLACE(CONCAT(',', '4,2,3,4,5,6,7,6,8,4'), ',4', ''));
Result:
2,3,5,6,7,6,8
I have one table - staff
id | staff Name | adress
-------------------------
1 | Mr.A | Any Address
2 | Mr. B | Any Address
2nd Table - employment_history
eid | staff_id | school_id | type | grade | date_of_appointmet
--------------------------------------------------------------------
1 | 1 | 1 |Promotion | 17 | 2012-12-12
2 | 1 | 2 |promotion | 18 | 2013-2-2
3 | 2 | 2 |appointment | 17 | 2013-3-3
and so on tables moves
Now the Question is that
i want to get the latest job of the person with his details from the staff table
how can i count how many of 17 grade staff works in school_id 1
(remembering that staff_id 1 (mr.a) now have been promoted to 18 and now works in school_id 2.)
select staff_id, max(date_of_appointment) as date_of_appointment
from employment_history
group by staff_id
This query will return the most recent staff record for each staff_id. Turn it into a subquery and join onto the employment history table
Select grade, count(1)
from
(select staff_id, max(date_of_appointment) as date_of_appointment
from employment_history
group by staff_id) a
inner join employment_history e on e.staff_id = a.staff_id and a.date_of_appointment = e.date_of_appointment
group by grade
This solution makes the assumption that staff_id + date_of_appointment is a unique key...if you have multiple rows where one staff_id has multiple employment history entries for one date, this won't work. You need some logic to make the 'most recent employment history entry' return a unique combination of data...if staff_id + max(date_of_appointment) is not unique, you'll need to come up with logic in the 'a' subquery that returns unique data.
What about something like
SELECT *
FROM employment_history eh1
WHERE eh1.date_of_employment = (
SELECT max(eh2.date_of_employment)
FROM staff s
JOIN employment_history eh2 ON s.id = eh2.staff_id
WHERE s.id = ?
)
Recplacing the ?, or using bind_param() as appropriate.
In a table i have some of column have duplicate values i want to retrieve unique values from my table i used SELECT DISTINCT column_name FROM table_name query and i got unique columns but my problem is i also want id of anyone of the duplicate value how can i retrieve that from using a single query ?
Eg
+----+------+------+
| id | name | po |
+----+------+------+
| 1 | some | 2 |
| 2 | xyzs | 3 |
| 3 | frth | 2 |
| 4 | lopd | 3 |
| 5 | gtry | 2 |
+----+------+------+
i want to find unique po and any one of its id
Output
some thing like this
po - 2 id - ( any of 1,3,5)
po - 3 id - ( any of 2 or 4)
Just group them and get the max id or the min.
SELECT max(id), po FROM table_name group by po
try this:
SELECT MIN(id) id, po
FROM table_name
GROUPB BY po, id
Don't quote me on this, but you might be able to do something like:
SELECT GROUP_CONCAT(id) FROM table_name GROUP BY po
If you don't care which id you will get, then:
SELECT po,id FROM table GROUP BY po
If you wish to get first/last of the ids with that same po, you can add MIN(id)/MAX(id) as well:
SELECT po,MIN(id) as id FROM table GROUP BY po
You can also have all the ids for that po:
SELECT po,GROUP_CONCAT(id) as ids FROM table GROUP BY po
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