SQL exists 2 table - mysql

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';

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.

I want to select data in 1 table

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.

Concatenating all matches from a join table into a column

I have two MySQL tables (table_a and table_b) and a join table (table_c).
Table Structures:
table_a:
__________________
| table_a: |
|----------------|
| id |
| result_column |
------------------
table_b:
__________________
| table_b: |
|----------------|
| id |
| name |
------------------
table_c:
__________________
| table_c: |
|----------------|
| id |
| table_a_id |
| table_b_id |
------------------
My Goal:
I want to find a query that will:
Iterate over every table_a record and get the table_a.id value
Find any records in table_c which have a matching table_c.table_a_id value
For each matching record in table_c get the table_c.table_b_id value
Find the record in table_b which has a matching table_b.id value
For that matching record in table_b get the table_b.name value
In table_a, concatenate each matched name value into the corresponding table_a.result_column
Example:
Before the Query:
_______________________ _________________________________ ________________
| table_a: | | table_c: | | table_b: |
|---------------------| |-------------------------------| |--------------|
| id | result_column | | id | table_a_id | table_b_id | | id | name |
|-----|---------------| |-----|------------|------------| |-----|--------|
| 1 | | | 1 | 1 | 3 | | 1 | Kevin |
| 2 | | | 2 | 1 | 4 | | 2 | Jesse |
| 3 | | | 3 | 2 | 2 | | 3 | Karen |
----------------------- | 4 | 3 | 1 | | 4 | Tim |
| 5 | 3 | 5 | | 5 | Lauren |
--------------------------------- ----------------
After the Query:
_______________________ _________________________________ ________________
| table_a: | | table_c: | | table_b: |
|---------------------| |-------------------------------| |--------------|
| id | result_column | | id | table_a_id | table_b_id | | id | name |
|-----|---------------| |-----|------------|------------| |-----|--------|
| 1 | Karen, Tim | | 1 | 1 | 3 | | 1 | Kevin |
| 2 | Jesse | | 2 | 1 | 4 | | 2 | Jesse |
| 3 | Kevin, Lauren | | 3 | 2 | 2 | | 3 | Karen |
----------------------- | 4 | 3 | 1 | | 4 | Tim |
| 5 | 3 | 5 | | 5 | Lauren |
--------------------------------- ----------------
For absolute clarity, I understand that this is incredibly bad practice within a relational data-table. This is as far from normalization as one can get. I would never design a database like this. I was tasked with creating a custom column with a list of values purely for a business case.
The query you seem to want is:
select c.table_a_id, group_concat(b.name separator ', ')
from c join
b
on c.table_b_id = b.id
group by c.table_a_id;
If you actually want to update a, you can put this into an update statement:
update a join
(select c.table_a_id, group_concat(b.name separator ', ') as names
from c join
b
on c.table_b_id = b.id
group by c.table_a_id
) cb
on cb.table_a_id = a.id
set result_column = cb.names
Previous answer is close; but you also required that you only want the records matched in table C that are in A.
The first query does not meet this requirement; but the update statement does, as it will only update records in table A, if the id matches the table_a_id value pulled from table C.
Given what you said you wished for the end result, the update statement above would work.
If you wish to be explicit in your logic, just add a join from table A to table C.
select a.id, group_concat(b.name separator ', ')
from a
join c ON (a.id = c.table_a_id)
join b ON (c.table_b_id = b.id)
group by a.id;

How to select records from one table in which other record should not be present in it

I have two tables. SubjectTbl and SubjectAllotTbl.
This is my SubjectTbl
+------------+--------------+---------------------+--------------+--------+----------+
| Subject_Id | Subject_Code | Subject_Name | Periods_Week | Dep_Id | Semester |
+------------+--------------+---------------------+--------------+--------+----------+
| 1 | 6012 | Basic Electronics | 10 | 1 | 1 |
| 2 | 5412 | Computer Science | 7 | 1 | 3 |
| 3 | 1421 | Musical Science | 4 | 1 | 5 |
| 4 | 547 | Network Programming | 7 | 1 | 5 |
+------------+--------------+---------------------+--------------+--------+----------+
and SubjectAllotTbl
+------------+------------+
| Subject_Id | Teacher_Id |
+------------+------------+
| 1 | 1 |
| 2 | 3 |
| 3 | 2 |
+------------+------------+
All i want to do is selecting Subject_Name from table SubjectTbl in which Same subject_id Should not be present in the SubjectAllotTbl
I'm using MySql
select subject_name
from SubjectTbl
where subject_id not in ( select distinct subject_id from SubjectAllotTbl)
You probably don't need distinct, unless subject_id is repeating in SubjectAllotTbl for different teacher_id
You can do that with a left join: all the rows that don't join will have null values on SubjectAllotTbl columns, and so you filter the result on that.
select subject_name
from SubjectTbl t1
left join
SubjectAllotTbl t2
on t1.subject_id = t2.subject_id
where t2.subject_id is null;

MySQL joining 3 tables using UNION

This question is a little long so that it would be clear, thanks in advance!
Introduction
I currently have 3 tables using a many-to-many relationship. I need to query all 3 tables and combine them into 1 table.
Problem
I have tried this query:
SELECT * FROM `login` LEFT JOIN membership ON login.id = membership.login_id UNION SELECT * FROM `login` RIGHT JOIN membership ON login.id = membership.login_id
And it returns:
+----+------+----------+
| id | name | group_id |
+----+------+----------+
| 1 | Tom | 6 |
| 2 | John | 8 |
| 3 | Jane | 4 |
+----+------+----------+
Question
I need it to also include the group_name. This is my desired output:
+----+------+----------+------------+
| id | name | group_id | group_name |
+----+------+----------+------------+
| 1 | Tom | 6 | Red |
| 2 | John | 8 | Brown |
| 3 | Jane | 4 | Purple |
+----+------+----------+------------+
Tables
login Table
A list of all users with auto-increment id
+----+------+
| id | name |
+----+------+
| 1 | Tom |
| 2 | John |
| 3 | Jane |
+----+------+
group Table
A list of all groups with the group_id and group_name
+----------+------------+
| group_id | group_name |
+----------+------------+
| 1 | Green |
| 2 | Blue |
| 3 | Yellow |
| 4 | Purple |
| 5 | Orange |
| 6 | Red |
| 7 | Pink |
| 8 | Brown |
+----------+------------+
membership Table
Stores information on which user belongs to which group
+----------+----------+
| login_id | group_id |
+----------+----------+
| 1 | 6 |
| 2 | 8 |
| 3 | 4 |
+----------+----------+
Join the group table as well and select the required fields from the tables.
SELECT l.id,l.name,m.group_id,g.group_name
FROM `login` l
LEFT JOIN `membership` m ON l.id = m.login_id
LEFT JOIN `group` g on g.group_id = m.group_id
Try this... :)
SELECT
`login`.`id`,
`login`.`name`,
`group`.`group_id`,
`group`.`group_name`
FROM
`membership`
INNER JOIN `login` ON (`membership`.`login_id` = `login`.`id`)
INNER JOIN `group` ON (`membership`.`group_id` = `group`.`group_id`)