I´m trying to create a select from 3 different tables.
I want to show the name of the student, his class and the date of register.
I have 3 tables "aluno" (student), "turma" (class) and "data..." (date).
The name of the student is on the first table named "aluno".
The class of the stundet is on the table named "turma".
The register on the class is on the table named "matricula".
My objetive is to show the name o f the stundent, the name of the class (designacao) and the date (data).
My function that does this sql command is doing a join, but i´m not sure if its ok..
I think the select is wrong.
function DBRead15()
$sql="SELECT aluno.nome as nome, matricula.*, turma.*
FROM matricula
LEFT JOIN aluno ON aluno.n_processo = matricula.n_processo";
$result=DBExecute($sql);
while($res=mysqli_fetch_assoc($result))
{
$data[]=$res;
}
return $data;
Looking to your tables
SELECT aluno.nome as nome, matricula.*, turma.*
FROM matricula
LEFT JOIN aluno ON aluno.n_processo = matricula.n_processo
LEFT JOIN turma ON matricula.turma = turma.idturma
You started doing the query partially, but can continue as follows:
SELECT aluno.nome as nome, turma.designacao, matricula.data
FROM aluno
LEFT JOIN turma ON aluno.n_processo = matricula.n_processo
LEFT JOIN matricula ON turma.idturma = matricula.turma
This query assigns the connection between aluno and turma as aluno_id (one-to-many relationship) and the connection between turma and matricula as turma_id (one-to-many relationship). This query may not work perfectly for you, you will have to change aluno_id to whatever column connects your students to their classes.
This query is an example of how table JOIN in SQL.
Related
Hello everyone I have a problem that I need help with I'm trying to get number of exam for specified student
we have three tables :
etudient (N_Appoge,Nom,PRENOM,filiere,....)
module (NUM_Module,Nom_module,Nom_semestre)
Inscrit (N_Appoge,NUM_Module) // this table is the relation between etudiant & module
so basically students with the same class means they have the same filiere and the same Nom_semestre
I tried this query :
set #num_exam=0;
SELECT DISTINCT N_APPOGE, NOM, PRENOM, filiere,Nom_semestre,
(#num_exam:=#num_exam+1) as num_exam FROM etudiant,
module
WHERE
filiere in (SELECT filiere FROM etudiant WHERE N_APPOGE='1610065' )
And
Nom_semestre In
(SELECT Nom_semestre from module WHERE Nom_semestre='s1'
and NUM_module in (SELECT NUM_module from inscrit where N_APPOGE='1610065'))
ORDER by NOM;
but the result was not like I expected it shows two result of the same student with N_APPOGE='1610065' and multiple result for other student as well
It looks like DISTINCT in this case does not work what's the solution here ?
You have a join between etudiant and module, but in the WHERE clause there is nothing which specifies how these two tables are related. As such, the database is performing a cross join (each row in the first table is joined to each row in the second table). Apparently you need to get the table inscrit involved:
SELECT DISTINCT e.N_APPOGE,
e.NOM,
e.PRENOM,
e.filiere,
m.Nom_semestre,
(#num_exam:=#num_exam+1) as num_exam
FROM etudiant e
INNER JOIN inscrit i
ON i.N_Appoge = e.N_Appoge
INNER JOIN module m
ON m.NUM_Module = i.NUM_Module
WHERE e.filiere in (SELECT filiere
FROM etudiant
WHERE N_APPOGE='1610065') And
m.Nom_semestre In (SELECT Nom_semestre
from module
WHERE Nom_semestre='s1' and
NUM_module in (SELECT NUM_module
from inscrit
where N_APPOGE='1610065'))
ORDER by NOM
Hopefully that will help.
I have a mySQL db named relOwner that has two columns:
OwnerID, RelationshipOwner
I am writing a query with joins that references the db:
$query = "SELECT b.Contact, b.ContactB, relOwner.OwnerID, relOwner.RelationshipOwner
FROM b
Left JOIN relOwner
ON b.Contact = relOwner.OwnerID
Left JOIN relOwner
ON b.ContactB = relOwner.OwnerID
";
How do I reference the values of RelationshipOwner individually in my php?
$RelationshipOwner = $row['RelationshipOwner'];
$RelationshipOwnerB = $row['RelationshipOwner']; <--- Get value from second JOIN
Thanks in advance.
It seems that you have two foreign key columns on table b to table relOwner (viz Contact and ContactB).
As per Sverri's comment, you will need to use a different alias for the tables (I've used ro1 and ro2) , and project different names from the different table columns (e.g. prefix the second table columns with ro2):
SELECT b.Contact, b.ContactB, ro1.OwnerID, ro1.RelationshipOwner,
ro2.OwnerID as ro2OwnerId, ro2.RelationshipOwner as ro2RelationshipOwner
FROM b -- Is this table Contact? If so then "Contact b"
Left JOIN relOwner ro1
ON b.Contact = ro1.OwnerID
Left JOIN relOwner ro2
ON b.ContactB = ro2.OwnerID;
Which you can then reference:
$row['ro2RelationshipOwner'];
I am trying to join a dataofbirth column which is in studentdetails table with another table called college table, I need the dataofbirth of the student in the second table.Here is my query.
$result = mysqli_query($con,"SELECT `DateofBirth` FROM `studentdetails` INNER JOIN `college`
ON `studentdetails`.StudentID = college.StudentID")or die ("Error: ".mysqli_error($con)");
So when I fetch my array for the second table I would be able to get their dataofBirth of student without joining them physically.
Can anyone spot what's wrong with my syntax ?
Thank you
If you want to join two tables you have to provide the list of columns from both tables that you want to be included in joined/combined table. So:
Try this (substituting [add other columns..] bit with the other columns you might want to include. And take note of the table aliases how they are being used:
$result = mysqli_query($con,"SELECT s.DateofBirth, c.StudentID, [add other columns from college table here...] FROM studentdetails sd INNER JOIN college c
ON sd.StudentID = c.StudentID")or die ("Error: ".mysqli_error($con)");
oh and you might want to use mysqli instead of old, deprecated, ugly, slow, insecure, suicidal thoughts inducing mysql_ extension ;)
It should be (from the comment : dateofbirth only exists in the studentdetails )
$result = mysqli_query($con,"SELECT sd.DateofBirth FROM studentdetails as sd INNER JOIN college as c
ON sd.StudentID = c.StudentID")or die ("Error: ".mysqli_error($con)");
Considering that we born only once (except for phoenixes) the relationship between students and their date of birth is supposed to be 1:1 relationship. In other words, column DateOfBirth could be declared in college table. Then to select their date of birth you simple could do:
select DateOfBirth
from college
where StudentID = :id
But I see you already have another table to store their date of birth so you could do a join to select their date of birth:
select d.DateOfBirth
from college c,
studentdetails d
where c.StudentID = d.StudentID;
Or:
select d.DateofBirth
from studentdetails d
inner join college as c
on d.StudentID = c.StudentID;
But this queries above would exclude the students without records in studentdetails from your result. To select all students with their respective date of birth you could do:
select c.StudentID,
s.DateOfBirth
from studentdetails d
right join college c
on d.StudentID = c.StudentID;
This right join query gets all the students even if they don't have a date of birth in studentdetails table (right join cmd on table college).
You're free to figure out what's the best alternative to you, but I recommend you to normalize this data model putting column DateOfBirth inside college table.
I have two tables and joined them to one different table
1 table named 'rec_dept'
id_dept
id_divisi
nama_dept
2 table named 'rec_divisi'
id_divisi
nama_div
3 joined table named 'rec_divdep'
id_divdep
id_divisi
id_dept
How to get nama_dept where in the same id_divisi?
Maybe you're looking for this:
SELECT `nama_dept` FROM `rec_dept` WHERE `id_divisi` IN (SELECT `id_divisi` FROM `rec_divdep`);
Hope that helps
you can do a SELECT query with a LEFT JOIN function to get data
SELECT a.`nama_dept` FROM `rec_dept` a
LEFT JOIN `rec_divisi` b
ON a.`id_divisi` = b.`id_divisi`
ORDER BY a.`id_divisi` ASC
SELECT documentation
LEFT JOIN documentation
select a.id_dept, a.id_divisi, a.nama_dept, b.id_divisi, b.nama_div, c.id_divdep, c.id_divisi from rec_divdep as c left join rec_divisi as b on (c.id_divisi = b.id_divisi) left join rec_dept as a on (c.id_divisi = a.id_divisi)
what database do you use. I code mine as mysql, basically I condition the three tables that has same id_divisi. I did not test it but I am pretty sure of the logic based on what I understand.
I need to pull data from two tables, and it's a bit over my head :
The first tables contains a list of members (member_id, username, email ...)
The second table stores relations between members (id, member_id, friend_id)
When a member adds another member as a friend, both member_ids are stored in the second table.
Now I need to output that second table, I'd like to output usernames instead of numbers :
example :
{username corresponding to member_id} added {username corresponding to friend_id} as a friend
Can someone help with the query ?
You need to perform a double join on members
SELECT mem1.username, mem2.username
FROM members mem1
INNER JOIN relations
ON mem1.member_id = relations.member_id
INNER JOIN member mem2
ON relations.friend_id = mem2.member_id
Something like:
select tb1.username as member_name,
tb2.username as friend_name
from membertable as tb1
inner join
(
membertable as tb2,
memberrelationstable
)
on
(
tb1.member_id = memberrelationstable.member_id and
tb2.member_id = memberrelationstable.friend_id
)
This is how I would do it:
SELECT member.username AS member_username, friend.username AS friend_username
FROM relations
INNER JOIN members AS member
ON relations.member_id = member.member_id
INNER JOIN members AS friend
ON relations.friend_id = mem2.member_id
I've spaced it so that you can easily see how we're joining the members table twice, and simple giving it a different name both times.
Whenever something is followed by AS, it means that you're giving it another name. This allows you to use the same table multiple times in a single query.