Is there any other way to extract the number of exam? - php

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.

Related

Getting data from a table even if there's no data in table 2 that links to table 1

I'm creating a planner but the SQL statement I have now only shows employees that have something in table 2 (plannerEntries) and doesn't show the rest of the employees from table 1 (Employee_List).
I need all of the employees to be outputted into the table regardless of whether they have any jobs assigned for them for the week, so that they can have new jobs assigned easily.
This is my current SQL code
SELECT [EL].[Employee_Numer],
[PP].[workDate],
[PP].[jobNo],
[PP].[workDescription],
[PP].[timeOfDay],
[JF].[Description],
[EL].[Forename],
[EL].[Surname]
FROM plannerEntries AS PP
RIGHT JOIN [Employee_List] AS EL
ON [PP].[employeeNumber] = [EL].[Employee_Numer]
INNER JOIN [Job File] AS JF
ON [PP].[jobNo] = [JF].[Job No]
WHERE [PP].[workDate] >= '$monday'
AND [PP].[workDate] <= '$sunday'
ORDER BY [PP].[employeeNumber] ASC;
I expected all employees to be printed regardless of records in table 2, however only the employees with records in table 2 were printed. The below image is the actual output.
Please check the difference between inner join, left join, right join.
Something like this should do what you need:
SELECT
[EL].[Employee_Numer],
[PP].[workDate],
[PP].[jobNo],
[PP].[workDescription],
[PP].[timeOfDay],
[JF].[Description],
[EL].[Forename],
[EL].[Surname]
FROM
[Employee_List] AS EL
left join plannerEntries AS PP on [PP].[employeeNumber] = [EL].[Employee_Numer]
and [PP].[workDate] >= '$monday'
and [PP].[workDate] <= '$sunday'
left join [Job File] AS JF on [JF].[Job No] = [PP].[jobNo]
ORDER BY
[PP].[employeeNumber] ASC;

Create a select from 3 tables

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.

select two tables and view in a table

I’ve a BD with the next tables.
TABLE detalle_contrato
TABLE detalle_tradicional
There is a relation with ID_CONTRATO and i need to view the table with the next data.
SELECT
ID_CONTRATO,
TRADICIONAL,
NOM_VARIEDAD,
SUM(CANTIDAD)
FROM detalle_contrato
WHERE ID_CONTRATO = '$ID' AND TIPO_VARIEDAD = 'TRADICIONAL';
SELECT
SUM(CANTIDAD_D)
FROM detalle_tradicional
WHERE ID_CONTRATO = '$ID'
GROUP BY NOM_VARIEDAD ";
There are a filter different in this two select and I need this in a table but i don't know together.
The idea is this:
ID_CONTRATO,
NOM_VARIEDAD,
CANTIDAD
( THIS IS THE SUM THE ALL CANTIDAD DUKE AND
LEGACY IN GROUP THE TABLE DETALLE_CONTRATO) ,
CANTIDAD_D
(TABLE DETALLE_TRADICIONAL THIS IS SUM
THE ALL DUKE AND LEGACY SEPARATE THE CANTIDAD_D
I need exactly this using the data the photos
You can use LEFT JOIN. Left join your second table with id_contrato and detalle_contrato id_contrato.
SELECT
dc.ID_CONTRATO,
dc.TRADICIONAL,
dc.NOM_VARIEDAD,
dc.IFNULL(SUM(CANTIDAD),0) AS CANTIDAD,
dc.IFNULL(SUM(CANTIDAD_D),0) AS CANTIDAD_D
FROM
detalle_contrato dc
LEFT JOIN TABLE_NAME t2 ON t2.ID_CONTRATO = dc.ID_CONTRATO
WHERE dc.ID_CONTRATO = '$ID' AND t2.TIPO_VARIEDAD = 'TRADICIONAL'

Sum mysql only works for the first row or just get wrong

I got this:
SELECT `cuenta`.`reservaId`, sum(`cuenta`.`tarifaTotal`)
FROM `check`,`cuenta`,`comanda`,`reserva`
WHERE `comanda`.`id`=`cuenta`.`idComanda` AND `comanda`.`tipo`=0
GROUP BY `cuenta`.`reservaId` ORDER BY `check`.`id`
the problem is that I get something like 10000000 as result of the first "reservaID" over the 119 I should get.
When I use this code, it works, but Only for the first "reservaID" space:
SELECT `cuenta`.`reservaId`, sum(`cuenta`.`tarifaTotal`)
FROM `check`,`reserva`,`cuenta`,`comanda`
WHERE `cuenta`.`reservaId`=`reserva`.`id` AND `cuenta`.`idComanda`=`comanda`.`id` AND `check`.`idReserva`=`reserva`.`id` AND `comanda`.`tipo`=0
GROUP BY `cuenta`.`reservaId`
ORDER BY `check`.`id`
tables:
check: id, reservaID
reserva: id, idPersona, idEmpresa, etc.
cuenta: reservaID, idComanda, Tarifatotal(this field is like mini bills, so "tarifatotal" is the price for each mini-bill)
comanda: id, name, tipo(or type, who can be 0,1,2 or 3) to describe the service type, like drinks, restaurant, services, others
Please help, I want to show all "sum"s for every single "reservaID".
Try this:
SELECT cuenta.reservaId, sum(cuenta.tarifaTotal) FROM check
JOIN reserva
ON reserva.id = check.idReserva
JOIN cuenta
ON cuenta.reservaId = reserva.id
JOIN comanda
ON comanda.id = cuenta.idComanda
WHERE comanda.tipo = 0
GROUP BY cuenta.reservaId
ORDER BY check.id;
Possibly a left join would solve your problems:
SELECT r.id, sum(c.tarifaTotal)
FROM reserva r LEFT JOIN
cuenta c
ON c.reservaId = r.id LEFT JOIN
commanda co
ON co.id = c.idCommanda
WHERE co.tipo = 0
GROUP BY r.id;
You don't seem to need the check table. Your ORDER BY doesn't make sense, because check.id is not in the SELECT list or GROUP BY list.

Mysql regroup results if one column result in common

I'm currently using 1-inf request in MySQL and I have a little problem right now.
Is it possible to join, concat, regroup some results who has the same column result in common ?
This are my ouptut
You have lessons (column one) who can be teach by teachers (column two) and the class (column three).
Is there a way to join them and have a result like
cours | professeurs1 professeurs2 professeurs3 | classe
Here is my query:
SELECT cours.cours, professeurs.nom, classe.classe
FROM cours
JOIN classe
ON cours.id_classe = classe.id
JOIN cours_professeurs
ON cours_professeurs.id_cours = cours.id
JOIN professeurs
ON cours_professeurs.id_prof = professeurs.id
Thanks in advance.
You can use GROUP_CONCAT and you will have to use group by with it too.
SELECT
cours.cours,
GROUP_CONCAT(professeurs.nom) AS teachers,
classe.classe
FROM cours
JOIN classe
ON cours.id_classe = classe.id
JOIN cours_professeurs
ON cours_professeurs.id_cours = cours.id
JOIN professeurs
ON cours_professeurs.id_prof = professeurs.id
GROUP BY cours.cours,classe.classe
Yes, you can do this using the GROUP BY() MySQL statement.

Categories