I want to select data in 1 table - mysql

structure table
Pk id <------------| primary Key
name |
country |
fk parent_id <-----| forigen key
Data
id| name | country | parent_id
1 | Diva | Portugal | 2
2 | Alex | Georgia | 2
3 | Bianca | Palau | 4
4 | Tony | Montenegro | 1
result
id| name | country | parent_id | name_parent_id |
1 | Diva | Portugal | 2 | Alex |
2 | Alex | Georgia | 2 | Alex |
3 | Bianca | Palau | 4 | Tony |
4 | Tony | Montenegro | 1 | Diva |
the result for this case
Any advice on this one?

You can use the same table twice in order to select the parent name:
SELECT
c.id,
c.name,
c.country,
c.parent_id,
p.name AS name_parent_id
FROM
YourTable AS c
INNER JOIN YourTable AS p ON c.id = p.parent_id
I used c as alias for the child and p for the parent.
Note: You can use LEFT JOIN instead of INNER JOIN in case of the existence of the parent is not required.

Related

Select rows that has common relationship

I have the following table:
Book
| id | book_title |
|----|------------|
| 1 | a |
| 2 | b |
| 3 | c |
| 4 | d |
Teacher:
| id | teacher_name |
|----|--------------|
| 1 | a |
| 2 | b |
| 3 | c |
| 4 | d |
Education stage:
| id | education_stage_name |
|----|----------------------|
| 1 | a |
| 2 | b |
| 3 | c |
| 4 | d |
The relationship between book and education stage:
| book_id | education_stage_id |
|---------|--------------------|
| 1 | 1 |
| 1 | 2 |
| 2 | 3 |
| 3 | 4 |
And the relationship between book and teacher:
| book_id | teacher_id |
|---------|------------|
| 1 | 1 |
| 1 | 4 |
| 2 | 3 |
| 3 | 4 |
Now I want to select books, which has the same education stages as a teacher with id=1
So the result table should look like this:
| book_id | title |
|---------|-------|
| 1 | a |
Because only the education stage with id=1 is assigned to the book and the teacher with id=1 at the same time.
Here is the fiddle, but I don't know even how to start with this query.
Is there anyone that has some ideas on how to write this query?
Is it possible to write it with DQL language?
This doesn't take anything complex, if all records will always exist, you can inner join on all tables:
select
Book.id as book_id,
Book.book_title,
Teacher.id as teacher_id,
Teacher.teacher_name
from Book
inner join book_education_stage bes on bes.book_id = Book.id
inner join Education_Stage es on es.id = bes.education_stage_id
inner join teacher_education_stage tes on tes.book_id = Book.id
inner join Teacher on tes.teacher_id = Teacher.id
Result:
book_id
book_title
teacher_id
teacher_name
1
a
1
a
1
a
1
a
1
a
4
d
1
a
4
d
2
b
3
c
3
c
4
d
Here is the DB Fiddle if you're interested.

SQL exists 2 table

i have 2 table like this
student
|-----------------------|
| id | name | value |
|-----------------------|
| F01 | Ruben | 4 |
| F02 | Dani | 2 |
| F03 | Mike | 3 |
| F04 | John | 4 |
|-----------------------|
tutor
|---------------------------------|
| id | code | student_id | class |
|---------------------------------|
| 1 | S2244 | F01 | IF-B |
| 2 | S3251 | F02 | IF-B |
| 3 | S2244 | F03 | IF-A |
| 4 | S2244 | F04 | IF-C |
|---------------------------------|
note, tutor.code ( S2244 and S3251) is foreign key from another table, tutor.class ( IF-B, IF-A, IF-C ) is foreign key from another table too, tutor.student_id is foreign key from student table, how to make the two tables combined and produce a result as below?
|-------------------------------|
| id | name | value | class |
|-------------------------------|
| F01 | Ruben | 4 | IF-B |
| F03 | Mike | 3 | IF-A |
| F04 | John | 4 | IF-C |
|-------------------------------|
I want to take all the columns in the student table and combine them with the "class" column in the tutor table.
I have tried it myself but can only display data from the student table, and the "class" column of the tutor table cannot appear, this is the query I made
select s.*
from students s
where exists (
select 1 from tutor t where t.student_id = s.student_id and t.code = 'S2244'
)
I would just use an inner join here:
SELECT
s.id,
s.name,
s.value,
t.class
FROM student s
INNER JOIN tutor t
ON s.id = t.student_id
WHERE
t.code = 'S2244';

MySQL Select Join on Multiple Tables

I have multiple tables (tbldept, tblcourse, tblstud, tblviolation) and I want to extract specific values. The below tables is like the same on my tables
tbldept
id | dept
1 | deptA
2 | deptB
tbldept has foreign key on tblcourse
tblcourse
id | deptId | course
1 | 2 | courseA
2 | 1 | courseB
3 | 1 | courseC
tblcourse has foreign key on tblstud
tblstud
id | courseId | name
1 | 1 | studA
2 | 2 | studB
3 | 1 | studC
tblstud has foreign key on tblviolation
tblviolation
id | studId | violationName
1 | 3 | violationA
2 | 2 | violationB
3 | 1 | violationC
4 | 3 | violationC
*What I want to get is look like this: *
dept | studId | name | violationName
2 | 1 | studA | violationC
2 | 2 | studB | violationB
1 | 3 | studC | violationA
1 | 3 | studC | violationC
I want to get all the rows of tblviolation for each studId.
I hope you guys understand what I am trying to explain. =) Thank you.
You just need inner join. Try this.
select d.dept,s.studid,s.name,v.violationname
from tbldept d
inner join tblcourse c
on d.id=c.deptid
inner join tblstud s
on c.id=s.courseid
inner join tblviolation v
on s.id=v.studid

Select distinct ordered pair from table join

Please consider two tables with names. They are joined by Table A id, so that two names get associated.
Is there a MySQL query that returns distinct pair of names regardless the order?
First table:
table_a
+-----------+--------------+
| id | name |
+-----------+--------------+
| 1 | John |
+-----------+--------------+
| 2 | Jane |
+-----------+--------------+
| 3 | Jane |
+-----------+--------------+
| 4 | Sammy |
+-----------+--------------+
Second Table:
table_b
+-----------+-------------------+-------------+
| id | id_table_a | name |
+-----------+-------------------+-------------+
| 1 | 1 | Jane |
+-----------+-------------------+-------------+
| 2 | 2 | John |
+-----------+-------------------+-------------+
| 3 | 3 | Sammy |
+-----------+-------------------+-------------+
| 4 | 4 | Tara |
+-----------+-------------------+-------------+
Desired result
(John, Jane)
(Jane, Sammy)
(Sammy, Tara)
Thanks in advance!
Here's one option using least and greatest:
select distinct least(a.name, b.name), greatest(a.name, b.name)
from table_a a
join table_b b on a.id = b.id_table_a
SQL Fiddle Demo

Multiple Select Join query with corresponding column on one table to others

Lets say that I have something table like this:
<b>Name</b>
+---------+--------+
| name_id | name |
+---------+--------+
| 5 | Betti |
| 6 | Derry |
| 7 | Alfred |
| 8 | Elsie |
| 9 | Cinta |
+---------+--------+
<b>Goods</b>
+----------+-----------+
| goods_id | goods |
+----------+-----------+
| 1 | Computer |
| 2 | AC |
| 3 | Microwave |
| 4 | TV |
+----------+-----------+
<b>Transaction</b>
+-------+---------+----------+
| ai_id | name_id | goods_id |
+-------+---------+----------+
| 1 | 7 | 2 |
| 2 | 5 | 4 |
| 3 | 9 | 3 |
+-------+---------+----------+
I want to replace name_id column on Transaction table with name column on Name table with corresponding name_id column and so for goods_id to produce something similar to this table:
<b>Transaction</b>
+-------+--------+-----------+
| ai_id | name | goods |
+-------+--------+-----------+
| 1 | Alfred | AC |
| 2 | Betti | TV |
| 3 | Cinta | Microwave |
+-------+--------+-----------+
If you're looking to just display the information rather than actually "replacing" your tables with new ones, then you could use a SELECT query with a simple INNER JOIN. This way you can display columns from multiple tables.
SELECT T.ai_id, N.Name, G.goods
FROM Transaction T
INNER JOIN Name N ON N.name_id = T.name_id
INNER JOIN Goods G ON G.goods_id = T.goods_id;
Try with inner join
SELECT T.ai_id,N.name,G.goods
FROM Transaction as T
INNER JOIN Goods as G ON T.goods_id = G.goods_id
INNER JOIN Name as N ON N.name_id = T.name_id;
Try this one
select tb3.ai_id,tb2.name,tb1.goods from Goods tb1,Name tb2,Transaction tb3 where tb3.name_id=tb2.name_id and tb3.goods_id=tb1.goods_id order by tb3.ai_id
try this:
SELECT C.ai_id,A.name,B.goods
FROM Transaction C
INNER JOIN Name A
ON A.name_id=C.name_id
INNER JOIN Goods B
ON B.goods_id=C.goods_id;
http://sqlfiddle.com/#!2/3c5f3/8