Forgive me, I'm still learning but am in need of some assistance. Some of what I’ve done is an amalgam of previous questions but I can’t find quite what I’m looking for.
I have a table with 30 columns of data, let’s call it table1. Every two columns are actually a set of the same type of data that have meaning together and singly. For example col1 with col2, is say a set of names.
Like this:
1 Jim Jeff
2 Mike Ben
3 Mike Mike
4 Peter Jeff
5 Jeff Jim
6 etc etc
The remaining 28 columns aren't important at this point. I want to return a single list of the unique names in col1 AND col2 along with their counts in total from both columns. Here’s what I have and it seems to work to a point but there is a problem with the return.
SELECT col1, COUNT(*)
FROM table1
GROUP BY col1
UNION
SELECT col2, COUNT(*)
FROM table2
GROUP BY col2
The problem is, when col1 has a name in it that is also in col2 it will return two counts. For example, if I had 6 different names, a total of 100 times, 50 in each column I might see something like this returned with the above query.
Jim 4
Jim 13
Jeff 8
Jeff 19
Mike 11
Mike 34
Ben 4
Brian 2
Peter 5
Obviously, Jim, Jeff and Mike appear in both columns and Ben, Brian and Peter appear in only one (It seems to me that it doesn’t matter which one).
What I need returned is:
Jim 17
Jeff 27
Mike 45
Ben 4
Brian 2
Peter 5
I tried putting a subquery in GROUP BY to force what is returned by a union without the count (forgive me, I don’t know much SQL, I'm just making assumptions by what little I understand of the language), meaning:
GROUP BY (SELECT col1 FROM table1 UNION SELECT col2 FROM table2)
but I guess I’m making silly assumptions. Any suggestions?
You can use a CTE to get the list of all names, then do a count based on that.
;WITH Names AS
(
SELECT col1 AS [Name]
FROM table1
UNION ALL
SELECT col2 AS [Name]
FROM table2
)
SELECT [Name], COUNT(*)
FROM Names
GROUP BY [Name]
Related
For example, let us consider this table:
In this image consists of rows of 8 where names like Mike,Glenn,Daryl,Shane and Patricia is included with their respective ages
Id
Name
Age
1
Mike
25
2
Glenn
19
3
Glenn
19
4
Daryl
56
5
Shane
30
6
Shane
30
7
Patricia
16
Now I want to insert the type of query that will show the names without repetitions like This, not like This
EDIT: I entered the data from first picture. The request is to list the names without duplicates, as shown in the second and third picture but I will not convert them to text.
DISTINCT specifies removal of duplicate rows from the result set.
SELECT DISTINCT Name
FROM tablename
see: use DISTINCT in SELECT
You can use GROUP BY to achieve it.
SELECT * FROM your_table
GROUP BY your_table.name
ORDER BY id
With the data you gave, the result from this query will be:
id
name
age
1
Mike
25
2
Glenn
19
4
Deryl
56
5
Shane
30
7
Patricia
16
I have following data
ID User1 User2 DeviceCode
1 John D1
2 John D2
3 Brian D3
4 Brian D4
5 Davis D5
6 Davis D5
For the above kind of table Layout in MySQL I am trying to figure out count of how many different DeviceCode each user is mapped to
Looking forward to the guidance.
Thanks
Use coalesce():
select coalesce(user1, user2) as user, count(distinct devicecode)
from t
group by user;
It seems odd to me that only one user column is populated per row. Why bother having two columns in that case?
I know this is probably so odd to ask. But lets say I have 3 tables:
Table 1
ID
Name
1
Adam
2
David
3
Conor
Table 2
ID
Name
1
Adam
2
Derek
3
Niall
Table 3
ID
Name
1
Adam
2
David
3
John
Is there any way I can write a query to get the unique names across all 3 tables. So it would return "Adam, David, Conor, Derek, Niall, John"
Order doesn't matter
If it helps, all name values are related to a names table
yes , one way is to union them
select name from table1
union
select name from table2
union
select name from table3
union automatically removes duplicate cases
I have row and column locations of several students.
Assuming number of rows and columns are fixed (to 3x3), how can I have a query result listing all row and column combinations, with students mapped to the correct location?
For example given these students data:
Student Row Column
Paul 1 1
Chris 1 3
James 2 2
Dwayne 3 3
How to have a query output like this:
Student Row Column
Paul 1 1
NULL 1 2
Chris 1 3
NULL 2 1
James 2 2
NULL 2 3
NULL 3 1
NULL 3 2
Dwayne 3 3
Please help! Thank you very much in advance.
While using PHP, Try mysql_insert_id() for your Code.
See Example here:
http://php.net/manual/en/function.mysql-insert-id.php
Good luck.
First of all, you need to know that Mysql haven't a implicit generator of N numbers of rows, like other RDBMS have, but you can emulate this using something like this:
http://use-the-index-luke.com/blog/2011-07-30/mysql-row-generator#mysql_generator_code
Take a look for study porpuse.
But for a first approach to resolve your problem, you can try this:
SELECT IFNULL((SELECT STUDENT FROM StudentSeatPlan B WHERE B.ROW = TB.ROW_ AND B.COLUMN = TB.COLUMN_),'') AS STUDENT,
TB.ROW_,TB.COLUMN_
FROM (
SELECT 1 ROW_,1 COLUMN_ UNION ALL
SELECT 1,2 UNION ALL
SELECT 1,3 UNION ALL
SELECT 2,1 UNION ALL
SELECT 2,2 UNION ALL
SELECT 2,3 UNION ALL
SELECT 3,1 UNION ALL
SELECT 3,2 UNION ALL
SELECT 3,3) TB
Whatever, it seems like you have a schema problem, something wrong it happens that you need generate data in this form in Mysql, maybe you prefered make it in your app if is the case.
I have table like this:
id products
------ ----------
5 1,2,3
6 2,4,5
9 1,4,7
17 4,6,7
18 1,6,8
19 2,3,6
I have to select only that rows, which row's products column contains one of (2,3) values.
In this case query must return:
id products
------ ----------
5 1,2,3
6 2,4,5
19 2,3,6
But I don't understand how to make construction of this query.
Any ideas?
Thanks in advance.
SELECT id,products
FROM yourTable
WHERE FIND_IN_SET('2',products)>0
OR FIND_IN_SET('3',products)>0
sqlFiddle
Would you mind to try this one please?
select * from TABLE_NAME where products regexp "(^|,)[23](,|$)";
Its doing either two or three at the begining, or at end. Or in between the commas.
Never, never, never store multiple values in one column.
Like you see now this will only give you headaches. Normalize your table. Then you can select normally.
Your table should look like this
id product
-- -------
5 1
5 2
5 3
6 2
6 4
6 5
...
With that table structure your select would be
select id
from your_normalized_table
where product in (2,3)
group by id
having count(distinct product) = 2
That query can make use of indexes and is really fast.