I use MySQL,
In my database, I have this table :
+-----------------------------+
| ID NAME ID_FATHER |
+-----------------------------+
| 1 Mylodi 0 |
| 2 Jack 0 |
| 3 Linda 1 |
| 4 Mark 2 |
| 5 Simon 4 |
| 6 Sacha 1 |
| 7 Edward 1 |
+-----------------------------+
By this query I retrieve each name with his father :
select f.name as father, s.name as son from family s, family f where s.id_father = f.id
Result
+------------+---------+
| father | son |
+------------+---------+
| mylodi | linda |
| Jack | mark |
| mark | simon |
| mylodi | sacha |
| mylodi | edward |
+------------+---------+
But, my question How can I get this result, but in one column like this :
+--------+
| colum |
+--------+
| Mylodi |
| linda |
| sacha |
| edward |
| jack |
| mark |
| simon |
+--------+
that mean I want to display the name of the Father and of below each name of father names of his son. Thanks.
EDIT
this is my db structure
How about something like this:
SELECT name FROM (
SELECT id, name, id sort1, id_father sort2 FROM family WHERE id_father = 0
UNION
SELECT id, name, id_father, 1 sort2 FROM family WHERE id_father <> 0
) AS v
ORDER BY sort1, sort2
Here's an updated fiddle.
This is not so straightforward. It assumes that there are no loops in the data (a is b's father and b is a's father)
Not sure what database you are using, but Oracle has Hierarchical queries that are meant for problems like this using the "connect by" keyword.
Related
I'm trying to merge two select sentence. Is it possible ?
I have three tables :
score
+------+---------+-------+
| ID | SUBJECT | SCORE |
+------+---------+-------+
| 1 | Chinese | 65 |
| 1 | English | 75 |
| 2 | Chinese | 60 |
| 2 | English | 70 |
| 3 | Chinese | 80 |
| 3 | English | 50 |
+------+---------+-------+
student
+------+----------+--------+
| ID | CLASS_ID | NAME |
+------+----------+--------+
| 1 | 1 | TOM |
| 2 | 1 | ANNA |
| 3 | 2 | JOHN |
+------+----------+--------+
class
+------+----------+
| ID | NAME |
+------+----------+
| 1 | 5th |
| 2 | 6th |
+------+----------+
mysql> select class.NAME as CLASS_NAME, student.NAME from class inner join student on student.CLASS_ID = class.ID;
+------------+--------+
| CLASS_NAME | NAME |
+------------+--------+
| 5th | TOM |
| 6th | ANNA |
| 6th | JOHN |
+------------+--------+
mysql> select SUM(SCORE) as total from score group by ID;
+-------+
| total |
+-------+
| 140 |
| 130 |
| 130 |
+-------+
Could I merge two select sentence let it be
+------------+--------+-------+
| CLASS_NAME | NAME | total |
+------------+--------+-------+
| 5th | TOM | 140 |
| 6th | ANNA | 130 |
| 6th | JOHN | 130 |
+------------+--------+-------+
Or is there any better search sentence to do this well?
I try use two sentence to merge , but can't have a good idea.
mysql> select class.NAME as CLASS_NAME, student.NAME from class inner join student on student.CLASS_ID = class.ID;
mysql> select SUM(SCORE) as total from score group by ID;
hope it can be merge success or have another answer to do this well.
You can use inner join to merge the second query. Below query takes in consideration that the join condition will be score.id with student.id
select c.name as class_name,
st.name ,
sc.total
from class c
inner join student st on st.class_id = c.id
inner join ( select id,
SUM(SCORE) as total
from score
group by id
) as sc on sc.id=st.id ;
https://dbfiddle.uk/tauTGBFO
I have 2 tables like this.
Table : Family Members
----------------------------------
|Address | Name |
----------------------------------
|North Jakarta City | Andra |
|North Jakarta City | Halim |
|South Jakarta City | Irma |
|Thousand Island Village | Dian |
----------------------------------
Table : Member Details
---------------
| Name | Age |
---------------
| Andra | 1 |
| Halim | 50 |
| Irma | 20 |
| Dian | 4 |
---------------
What is the correct query if I want to count members between the ages 0 and 4 who live in a 'city'? I've tried using this query but the result is incorrect. The correct result should be 1 since only Andra who lives in a city and between the ages 0 and 4. Please help me.
SELECT COUNT(family_members.name) AS total FROM family_members, member_details
WHERE family_members.address LIKE '%City%' AND member_details.age BETWEEN 0 AND 4
You need a join
SELECT COUNT(fm.name) AS total
FROM family_members fm
Join member_details md on md.Name = fm.Name
WHERE fm.address LIKE '%City%' AND md.age BETWEEN 0 AND 4
with you syntax, you may add this in the where clause (because your query will generate a cartesian product).
BUT : you should really use the JOIN syntax
AND family_members.Name = member_details.Name
EDIT
By the way, I would strongly suggest to use surrogate keys in your tables (a name is not really something unique)
You should consider redesigning your database like:
----------------------------------
| user_id | Name | Age | city_id |
----------------------------------
| 1 | Andra | 1 | 1 |
| 2 | Halim | 50 | 1 |
| 3 | Irma | 20 | 1 |
| 4 | Dian | 4 | 2 |
----------------------------------
------------------------------------
|city_name | city_id |
------------------------------------
|North Jakarta City | 1 |
|Thousand Island Village | 2 |
------------------------------------
SELECT COUNT(*) AS total
FROM family_member
JOIN city on family_member.city_id = city.city_id
WHERE city.city_name= 'City' AND family_member.age BETWEEN 0 AND 4;
Also you should add indexes.
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 |
+--------+----------+
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
I have a table called tags, like this:
+-------+----------+
| tagID | tagName |
+-------+----------+
| 1 | jewelery |
| 2 | gifts |
| 3 | asdf |
| 4 | fashion |
| 5 | diamonds |
+-------+----------+
Then a table called coupon_tags, like this:
+-------+----------+
| tagID | couponID |
+-------+----------+
| 1 | 1 |
| 2 | 1 |
| 3 | 2 |
| 4 | 2 |
| 5 | 3 |
+-------+----------+
And lastly, a table called coupons, here are the pertinent parts (id is the same as couponID elsewhere):
+----+-----------------+
| id | zone |
+----+-----------------+
| 1 | Los Angeles |
| 2 | Orange County |
| 3 | Los Angeles |
| 5 | Orange County |
| 6 | Orange County |
+----+-----------------+
What I need to write a query for: I want to get tagNames via the first table that correspond to the ordered list of the top 10 most used tagIDs in the second table, but it only looks through couponIDs that match another criteria - that the "zone" be a certain zone. In the end, only the top 10 tagNames from a certain zone will show. I've never done a triple-table query before, any help?
I'm trying to keep this purely SQL, as I had a partially working PHP solution but it was messy and very slow.
SELECT tags.tagName FROM
(SELECT tagID, COUNT(*) FROM
coupon_tags
JOIN coupons ON coupons.couponID = coupon_tags.couponID AND zone = 'Los Angeles'
GROUP BY tagID ORDER BY COUNT(*) DESC LIMIT 10) AS most_used
JOIN tags ON most_used.tagID = tags.tagID