MySql: How to display only 1 name only per multiple name - mysql

To make it more clear, If I have this data in MySql:
name | allowance | age
----------------------
khan | 50 | 20
aal | 60 | 22
hyme | 50 | 21
khan | 61 | 20
notice that there are two 'khan' in the database with different allowance. I want to only show the name and the age but if I show it using the mysqli select statement, there would be two 'khan' but I only want to show only 1 'khan'. How can I do it?

You need to use GROUP_CONCAT to see agges of all Khans;
select name, GROUP_CONCAT(age) ages from Table group by name
or for minimum aged khan
select name , min(age) MiniumAge from Table group by name
or for elder khan
select name , max(age) MaxAge from Table group by name
or any khan
select name , age from Table group by name
.

Please try below query:-
SELECT name, age FROM table_name WHERE group by name
If you want any from multiple same record then simply used group by query.

I think you could do this:
SELECT name, age FROM table_name WHERE group by name,age

First thing: if both those "khan"s are the same person with two different allowances then your schema is not properly normalized and it will give you big troubles later - imagine you want to change "khan" to "Khan" - now you have to update it in multiple places instead once. Depending on your actual needs you may want one table of people (person_id, name, age), and table of allowances (person_id, allowance, [..some other parameters?..]).
Second, to really get what you want, either you use group by, to get one "random" row per each name as suggested in other answers, or you can do
SELECT DISTINCT name, age FROM table;
which will give you one row per each name-age combination, so khan-20 will be there only once - but if there were khan-25 then that is probably different person and you would have two khans returned, each with their own age.

You can try this mate:
SELECT DISTINCT
name, age
FROM
<your_table>;
or this one
SELECT
name, age
FROM
<your_table>
GROUP BY
name;
Q: Is there any chance that if there are 2 records of tha same name have difference value of age? If so, kindly update the question so that better answers will be given. Cheers!

Related

SQL Return value based on lowest value in another column

I've tried to search for a solution to this but have been unable to find one. I guess it's basic SQL, but I can't seem to figure it out.
I have a table called people, this table has several columns:
ID Firstname Lastname Birthdate
1 John Stevenson 1860-07-30
2 Eric Johnson 1918-08-25
3 Adam Efron 1914-02-02
4 Michael Gray 1870-07-18
Now I want to make a query that looks at the Birthdate column, finds the lowest value and returns the firstname of the person that has the lowest birthdate (is oldest).
Can someone guide me in the right direction?
THanks in advance!
Use order by like
select Firstname
from people
order by Birthdate
limit 1
This will account for 2 paople having the same birthdate, returning 2 rows.
select Firstname
from people
where birthdate = (select min(birthdate) from people);
Another way to do it efficiently would be this (sqlfiddle):
select p.*, min(p.birthdate) from people p;
NOTE: You will have 1 extra column in the output.

How to select rows in which data in a certain column is never repeated-SQL

I have a table with just 4 rows.
FIRST LAST AGE
luke david 42
jester prince 32
luke mike 16
sean paul 22
I want to select only those rows in which the first name is never repeated. So the result I need is
FIRST LAST AGE
jester prince 32
sean paul 22
The sql query should ignore all the rows in which the first name is being repeated. The simple distinct query doesn't work here because it just removes multiple entries. I want a query which can remove ALL the rows of the repeating first name.
TRY
SELECT * FROM `tbl_name` GROUP BY(`name`) HAVING COUNT(`name`) = 1
You can use group by if distinct does not work
SELECT * FROM Table GROUP BY FIRST
You may use a NOT IN clause, like this:
SELECT * FROM table1
WHERE first NOT IN (
SELECT first FROM table1
GROUP BY first
HAVING COUNT(*) > 1
)
Inner query finds all first names which are repeated and then using the NOT IN clause those duplicate first names are removed from the final result.
select FIRST, LAST, AGE
from MY_TABLE
group by FIRST
HAVING count(FIRST) = 1

Find lowest value and coresponding code on asingle query mysql

I have a user table as follows
id name age
1 John 21
2. Mathan 23
3. Raj 21
4. Manoj 50
5 Krishnan 91
I want to find minimum age and its corresponding name. How can I do it with rails?
Can I do it in a single query?
Note: More than one names can have single age.
Is there a specific reason why you want to do it in a single query ?
If you can write 2 queries, I think you can just write :
User.where age: User.minimum(:age)
select age, group_concat(name) from table group by age order by age asc limit 1
You will need to process the results later on in ruby, but this gives all you need in one single query. Also i am assuming mysql, so might differ on other rdbms.
It gives exact output in mysql that you want try this
SELECT concat("[",name," ",age,"]") AS name
FROM TABLE
WHERE age =
(SELECT min(age)
FROM TABLE);

find out count of comma based value in MySql

I have two tables.
Table Emp
id name
1 Ajay
2 Amol
3 Sanjay
4 Vijay
Table Sports
Sport_name Played by
Cricket ^2^,^3^,^4^
Football ^1^,^3^
Vollyball ^4^,^1^
Now I want to write a query which will give me output like
name No_of_sports_played
Ajay 2
Amol 1
Sanjay 2
Vijay 2
So what will be Mysql query for this?
I agree with the above answers/comments that you are not using a database for what a database is for, but here is how you could calculate your table from your current structure in case you have no control over that:
SELECT Emp.name, IF(Played_by IS NULL,0,COUNT(*)) as Num_Sports
FROM Emp
LEFT JOIN Sports
ON Sports.Played_by RLIKE CONCAT('[[:<:]]',Emp.id,'[[:>:]]')
GROUP BY Emp.name;
See it in action here.
UPDATE: added the IF(Played_by IS NULL,0,COUNT(*)) instead of COUNT(*). This means that if an employee doesn't play anything they'll have a 0 as their Num_Sports. See it here (I also added in those ^ characters and it still works.
What it does is joins the Emp table to the Sports table if it can find the Emp.id in the corresponding Played_by column.
For example, if we wanted to see what sports Ajay played (id=1), we could do:
SELECT *
FROM Emp, Sports
WHERE Sports.Played_by LIKE '%1%'
AND Emp.id=1;
The query I gave as my solution is basically the query above, with a GROUP BY Emp.name to perform it for each employee.
The one modification is the use of RLIKE instead of LIKE.
I use RLIKE '[[:<:]]employeeid[[:>:]]' instead of LIKE '%employeeid%. The [[:<:]] symbols just mean "make sure the employeeid you match is a whole word".
This prevents (e.g.) Emp.id 1 matching the 1 in the Played_by of 3,4,11,2.
You do not want to store your relationships in a column like that. Create this table:
CREATE TABLE player_sports (player_id INTEGER NOT NULL, sport_id INTEGER NOT NULL, PRIMARY KEY(player_id, sport_id));
This assumes you have an id column in your sports table. So now a player will have one record in player_sports for each sport they play.
Your final query will be:
SELECT p.name, COUNT(ps.player_id)
FROM players p, player_sports ps
WHERE ps.player_id = p.id
GROUP BY p.name;

select returns wrong id when using max

Assuming a table like this.
id town_id begin_date
12 2 2011-10-10
23 2 2011-11-10
43 2 2012-01-01
now if I do
SELECT id, MAX(begin_date) AS mx
FROM regions
The above query returns the max date but the id is wrong:
id mx
12 2012-01-01
Is this expected?
How can I get it to return the correct id (43, 2012-01-01)
If what you are trying to do is get the id associated with the MAX() date, then you can do:
SELECT id, begin_date from regions order by begin_date DESC LIMIT 1;
You forgot the GROUP BY clause:
SELECT id, MAX(begin_date) AS mx
FROM regions
GROUP BY 1
I also want to add another possible solution to this answer which might be more intuitive because the accepted answer actually is wrong and only the comment below solves it partly. Why? Because it only works if you want a grouped result by town_id. If you you need a solution which retrieves the row with the absolute maximum date you can only go with Francisco Sotos answer or the query below.
SELECT ID, begin_date from regions WHERE begin_date = (SELECT MAX(begin_date) FROM regions)
The query I posted does not use limit but instead requires a sub query. IDK which one is faster but just as an additional food for thought.