I'd like to have a query for the first and last name on an enrollment list so that only one result show. However, if only the last name is chosen in the query multiple answers will show.
You can GROUP BY the last name field in your query.
For example if you had this data:
MyTable:
id last_name first_name
1 Smith John
2 Smith Jane
3 Jones Paul
Running a query like this:
SELECT t.last_name
FROM MyTable t
GROUP BY t.last_name
ORDER BY t.last_name
...would return these two rows:
Jones
Smith
Related
If I have a table that is Names, which has just first name and last name as columns and I want to select everything (first and last names), but limit the number of repeated first names to 3. So for example,
First Name
Last Name
John
Smith
John
Doe
Ryan
Green
Mike
Williams
John
Thompson
John
Brown
becomes
First Name
Last Name
John
Smith
John
Doe
Ryan
Green
Mike
Williams
John
Thompson
How do I construct a query to do that?
You can use window functions. For instance:
select firstname, lastname
from (select t.*,
row_number() over (partition by firstname order by rand()) as seqnum
from t
) t
where seqnum <= 3;
Note: This returns random rows for each name. You can control which rows you get using the order by clause.
Hi everybody I need done help with this issue.
Example myDb tabele name people
ID name Sex
3 Adam Smith M
5 Sonia Kolan F
3 Donald Smith M
Select id, name from people where name LIKE ‚%Smith%’;
Result
Id Name
3 Adam Smith
3 Donald smith
My question is how to transform my result of col name to this view
Id name
3 Smith
3 Smith
I want too see in col name Expresion from like statement
I want too see in col name Expresion from like statement
Then just put that literal string in the select clause:
select id, 'Smith' name
from people
where name like '%Smith%'
You can join to the table a query that returns only 'Smith':
select p.id, t.name
from people p
inner join (select 'Smith' name) t
on p.name LIKE concat('%', t.name, '%');
See the demo.
Results:
> id | name
> -: | :----
> 3 | Smith
> 3 | Smith
I have a table like
Name Spouse
---------------
John Smitha
Bob Neetha
Neetha Bob
Mona Jack
Smitha John
Jack Mona
and I want results as below using joins in MySQL.
Name Spouse
---------------
John Smitha
Bob Neetha
Mona Jack
(i.e. the couple should be selected only once)
Assuming that you have a IsPrimary field.
You could easily achieve this with the following query.
SELECT P.NAME, S.NAME As SpouseName
FROM PEOPLE P
LEFT JOIN PEOPLE S ON P.Spouse = S.Spouse -- You should have a PK/FK relasionship here (SouseId) and not join on a name string
WHERE P.IsPrimary = 1
a way to do it like that
SELECT `Name`, `Spouse`
FROM Table1
WHERE `Name` <= `Spouse`
demo
Trying to wrap my head around this query, and have tried grouping, but no luck:
If I have a table:
name title (some other fields)
John Doe Engineer ...
John Doe Tech ...
John Doe Tech ...
Frank Smith Tech ...
Frank Smith Tech ...
I need a query that would result in:
name title count(title)
John Doe Engineer 1
John Doe Tech 2
Frank Smith Tech 2
Have tried using group by name and title, but it seems to just group one or the other, giving me the count of either total engineers (1) and techs (4), or total clients (2), but I need total titles BY name.
Any suggestions?
Don't know why wouldn't this work:
SELECT name, title, COUNT(*) Titles
FROM YourTable
GROUP BY name, title
try this
SELECT name , title , count(*) count
FROM Table1
GROUP BY name , title
ORDER BY count
DEMO SQLFIDDLE
SELECT NAME, TITLE, COUNT(*) FROM BOOKS GROUP BY NAME, TITLE
I have a table with columns id, user
I want to group by column id and show a list of users (comma separated) for each id.
In the final output I need to display:
user joe - id 1
users jim, mark, john - id 2
user dave - id 3
....
I'm getting error "Cardinality violation: 1242 Subquery returns more than 1 row" if I try this:
SELECT id, (SELECT distinct(user) FROM mytable b where a.id = b.id)
FROM mytable a
GROUP BY id
Since you are using MySQL, there is a built-in function for that which is the GROUP_CONCAT function. For example, you have a records like this:
ID User
1 Joe
2 Jim
2 Mark
2 John
3 Dave
If you try to run this query:
SELECT ID, GROUP_CONCAT(User) Users
FROM tableName
GROUP BY ID
you will then have a result like this:
ID Users
1 Joe
2 Jim, Mark, John
3 Dave
What you're looking for is the GROUP_CONCAT aggregate function:
SELECT id, GROUP_CONCAT(user) FROM table GROUP BY id