Mysql query to select only one unique name on a criteria - mysql

I have a table like this.
+------------+-------------+--------------+
| name | hobby | hobby_number |
+------------+-------------+--------------+
| jack | sport | 1 |
| marco | skydiving | 3 |
| alfonso | driving | 1 |
| marco | learning | 2 |
| jack | dancing | 2 |
+------------+-------------+--------------+
I want to use sql select statement to select only one unique name.
The table I want may look like this:
+------------+-------------+--------------+
| name | hobby | hobby_number |
+------------+-------------+--------------+
| jack | sport | 1 |
| marco | learning | 2 |
| alfonso | driving | 1 |
+------------+-------------+--------------+
What should sql query be?
Thank you in advance.

select t.* from your_table t
inner join
(
select name, min(hobby_number) as minh
from your_table
group by name
) x on x.name = t.name and x.minh = t.hobby_number

Related

Mysql use another table to translate value

table user
+-----+-------+----------+------------+
| id | name | gender | computer |
+-----+-------+----------+------------+
| 1 | ben | 1 | 3 |
+-----+-------+----------+------------+
table translate
+-----+--------+
| id | trans |
+-----+--------+
| 1 | boy |
| 2 | girl |
| 3 | pc |
| 4 | mac |
+-----+--------+
final result
+-----+-------+----------+----------+
| id | name | gender | computer |
+-----+-------+----------+----------+
| 1 | ben | boy | pc |
+-----+-------+----------+----------+
I have 2 tables, one is user's data, the other is translate.
I want to use translate table to translate user's gender & computer's values
anyone know how to achieve this?
Use JOIN.
Query
select
t1.`id`,
t1.`name`,
t2.`trans` as `gender`,
t3.`trans` as `computer`
from `user` t1
join `translate` t2
on t1.`gender` = t2.`id`
join `translate` t3
on t1.`computer` = t3.`id`;
The way you've presented this, it doesn't appear to be a translation problem (in the sense of language), but rather a standard database partition problem. A more standard solution would be to define your entities each in their own tables:
user
+-----+--------+------------+--------------+
| id | name | genderId | computerId |
+-----+--------+------------+--------------+
| 1 | ben | 1 | 1 |
+-----+--------+------------+--------------+
gender
+-----+--------+
| id | name |
+-----+--------+
| 1 | boy |
+-----+--------+
| 2 | girl |
+-----+--------+
computer
+-----+--------+
| id | name |
+-----+--------+
| 1 | pc |
+-----+--------+
| 2 | mac |
+-----+--------+
...and then your query reads:
SELECT *
FROM user
INNER JOIN gender ON user.genderId = gender.id
INNER JOIN computer ON user.computerId = computer.id

How connect columns based on the value of one field in MYSQL

I have a table looks like this:
|__name___|value__|
|__James__|___6___|
|__Jerry__|___5___|
|__Jerry__|___4___|
|__James__|___3___|
|__James__|___2___|
|__James__|___2___|
and I need to get from first table output like :
|_name____|value_|
| James | 2 |
| | 2 |
| | 3 |
|_________|___6__|
| Jerry | 4 |
|_________|___5__|
Any ideas?
this also works. It stores the last record in #old_name and compare it
SELECT
IF(name = #old_name ,'',(#old_name := name)) AS name
,value
FROM mytable
,(SELECT #old_name:='')AS tmp
ORDER BY name,value;
|_name____|value_|
| James | 2 |
| James | 2 |
| James | 3 |
| James | 6 |
| Jerry | 4 |
| Jerry |___5__|
only like this (select * from $table_name order by name;),
cant like this:
|_name____|value_|
| James | 2 |
| | 2 |
| | 3 |
|_________|___6__|
| Jerry | 4 |
|_________|___5__|
SELECT CASE WHEN
value = (SELECT MIN(value) FROM table t WHERE t.name = name)
THEN name
ELSE ''
END AS name, value
FROM table
ORDER BY name ASC, value ASC
Bernd Buffen.. Your Select return something like that :/ :
|_name____|value_|
|_________| 2 |
|_________| 2 |
|_________| 3 |
|_________| 4 |
|__James__| 5 |
|__Jerry__|___6__|

mysql same price mismatch column value

Good afternoon everyone,
I wonder if I can get help with this example in a database.
I have listings delivered as:
------------------------------------
| Id | name | price |
-------------------------------
| 1 | Hawaii | 20.58 |
| 2 | Hawaii VIP | 45.58 |
| 3 | Aruba | 13.58 |
| 4 | Aruba VIP | 34.58 |
| 5 | Japon | 14.58 |
| 6 | Japon VIP | 34.58 |
| 7 | Alemania | 14.58 |
| 8 | Alemania VIP | 14.58 |
But I need them to be shown as follows:
-----------------------------------------------------
| Id | name | price basic | price vip
-----------------------------------------------------
| 1 | Hawaii | 20.58 | 45.34 |
| 5 | Japon | 14.58 | 34.58 |
etc etc etc
What I need are the two prices of the same country in a different column in the same query.
As I can catch the "VIP" based on this field and put it in a column with its value
It would be better to store IS_VIP as a separate column in the first table instead of adding it to the name, but since it's not a perfect world, you could solve it like this until then. :)
SELECT
t.Id,
t.name,
t.price as "price basic",
v.price as "price VIP"
FROM YourTable t
JOIN YourTable v ON v.name = CONCAT(t.name, ' VIP')
Sql Fiddle Example

Counting the number of times something appears in a column and recording it

So in order to know how many people in a table are called Johnny I would need to excecute the following query.
Query:
Select count(*) from mytable where first = 'Johnny';
It would give me 2 as the result.
What I wish to do however is record this number in the count column so that the end result comes out like this.
+--------+----------+
| First | COUNT |
+--------+----------+
| Johnny | 2 |
| Diane | 1 |
| Johnny | 2 |
| Harold | 1 |
| Amy | 3 |
| Roy | 2 |
| Amy | 3 |
| Amy | 3 |
| Roy | 2 |
+--------+----------+
Is there any query or procedure capable of resulting in this type of output?
To get your exact output, you need to use a subquery:
select
mytable.First,
counts.`COUNT`
from
mytable
join (
select
First,
count(*) `COUNT`
from
mytable
group by
First
) counts on mytable.First = counts.First;
Try this:
SELECT T1.First, T2.COUNT
FROM mytable T1 JOIN
(SELECT First, COUNT(*) as COUNT
FROM mytable
GROUP BY First) as T2 ON T1.First=T2.First
The result will be:
+--------+----------+
| First | COUNT |
+--------+----------+
| Johnny | 2 |
| Diane | 1 |
| Johnny | 2 |
| Harold | 1 |
| Amy | 3 |
| Roy | 2 |
| Amy | 3 |
| Amy | 3 |
| Roy | 2 |
+--------+----------+

MySQL query DISTINCT, COUNT

I'm trying to use a SELECT DISTINCT to find out how many people have cars, how many have boats, now many bikes and so on, from this table
| name | obj |
---------------------
| john | car |
| mary | boat |
| dave | car |
| james | bike |
| steve | car |
| walt | bike |
ex: bike - 2
You can use this query
SELECT obj, COUNT(name) as nb FROM table GROUP BY obj ORDER BY obj
Result (sqlfiddle):
| obj | nb |
---------------------
| bike | 2 |
| boat | 1 |
| car | 3 |
select sum(obj = 'car') as car_count,
sum(obj = 'boat') as boat_count
from your_table