Select with two tables and one condition in each with MYSQL

Asked

Viewed 653 times

3

I have two unrelated tables. One is of reports, where it contains names of teachers and their respective subject, the other table is called login, where it contains credentials of each teacher, their privilege and also their respective subject.

The idea of the login table is that, with the access of the teacher, he can visualize all the teachers of the report table when their subject is equal to that of the registered teacher, and that also the privilege of the teachers is of teacher.

LOGIN TABLE

id
nome
materia
privilegio

REPORT TABLE

nome
materia

QUERY USED:

SELECT diga_relatorio.nome, diga_relatorio.materia, diga_login.materia 
FROM diga_relatorio, diga_login 
WHERE diga_relatorio.materia='{$_SESSION['digaPrivilegio']}' 
AND diga_login.privilegio='professor' 
GROUP BY diga_relatorio.nome 
ORDER BY diga_relatorio.nome

However, this way it is returned also the teachers with the position of coordinator, being wrong, since the hierarchy would be:

  1. COORDINATOR (view all others)
  2. SUPERVISOR (view only teachers of your subject)
  3. TEACHER (see for himself)

1 answer

3

When you want to relate one table to another through a field, you have to use one inner join and mention which field binds the tables, otherwise you make a outter join in which you unite everything with everything.

I removed the group by because I didn’t realize the usefulness in it in query.

Try:

SELECT diga_relatorio.nome, diga_relatorio.materia, diga_login.materia 
FROM diga_relatorio 
INNER JOIN diga_login 
ON diga_relatorio.materia = diga_login.materia 
WHERE diga_relatorio.materia='{$_SESSION['digaPrivilegio']}' 
AND diga_login.privilegio='professor' 
ORDER BY diga_relatorio.nome
  • The group by is grouping teachers, since in the table reports the name each repeats at least 500 times.. Anyway I will test the query and soon give a feedback. Until then, thanks!

  • the query returns me the same results always, regardless of the value I pass to diga_login='...'. The idea is that the supervisor can only see those teachers who have the privilege of 'teacher'.

  • The query was: "SELECT diga_reportario.nome, diga_relatorio.materia, diga_login.materia FROM diga_relatorio INNER JOIN diga_login ON diga_relatorio.materia = diga_login.materia WHERE diga_relatorio.materia='{$_SESSION['digaMateria']}' AND diga_login.privilegio='professor' GROUP BY diga_relatorio.nome ORDER BY diga_relatorio.nome"

  • Someone give a return, please!

  • I do not understand the usefulness of the report column since its two columns are repeated in the first table. Anyway for what you want to try: SELECT 
 diga_relatorio.nome,
 diga_relatorio.materia,
 diga_login.materia 
FROM 
 diga_relatorio 
INNER JOIN 
 diga_login 
ON 
 diga_relatorio.materia = diga_login.materia AND
 diga_relatorio.nome= diga_login.nome
WHERE 
 diga_relatorio.materia='{$_SESSION['digaPrivilegio']}' AND 
 diga_login.privilegio='professor' 
ORDER BY 
 diga_relatorio.nome

  • Which column report? I’m sorry, but I don’t see the cited column.. Ah, the instruction you gave me did not return results. What I am thinking of doing is registering all teachers in the table of diga_login accompanied by their privilege, since all teachers of the report table will be present in login

  • I wanted to ask about the usefulness of the report table. Confirm this: Is the column name of the report table equal to the column name of the login table? Is the report table’s matter column equal to the login table’s matter? It’s just that I think you have the logs from the login table, duplicated in the matter table. Otherwise put a piece of the comic you have, because it is confused.

  • Yes, that information is completely duplicated, and that is a problem that has existed for a long time. It turns out that these data are filled out per student, that is, a room with 40 students will create 40 records for a single teacher, since each teacher is evaluated by each student. All this was done in an excel, and now I’m exporting to MYSQL, and along with the import are coming several errors...

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.