B
B
Boris Yakushev2015-09-24 13:40:16
PHP
Boris Yakushev, 2015-09-24 13:40:16

Query to the database displaying common values ​​for different id?

Good afternoon.
There is a table: t_s (id, teacherid, studentid) I execute
a query on it:

SELECT teacherid, studentid, GROUP_CONCAT(studentid ORDER BY studentid ASC SEPARATOR ', ') FROM t_s GROUP BY teacherid

We get this conclusion:
UhUHjvW.png
We see that the fifth and ninth teachers have common students.
How to build a query so that the output would be like this (not necessarily that it will be the 5th or 9th teacher):
m4enw6R.png
Thank you very much in advance.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
B
Boris Yakushev, 2015-09-24
@za4me

The solution to this issue: TYTS
3rd request.

G
Gregory, 2015-09-24
@grigruss

The request does not need to be changed, and it will not work to do the one you write about. It is necessary to process the output of the request using PHP. Drive the output of the query into a multidimensional array, and use the foreach loop to search line by line for the presence of the same students in other lines, and then display them on one line. Something like this ... well, that's what I would do ...

G
gimntut, 2015-09-24
@gimntut

The request could be simpler if the second student was not registered with the first teacher 4 times. Requests: for teachers 5 and 9 and 1, 5 and 9 . The difference between requests is here .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question