I'm trying to write a MySQL query which should select rows from two different tables. I have made a different amount of queries but they all take pretty long time before they return the result (> 0.6 seconds). I want to have a even faster response.
The situation is:
CREATE TABLE `classes` (
id int(99) auto_increment primary key,
status INT(1)
)
CREATE TABLE `classes_names` (
id int(99) auto_increment primary key,
class_id int(99),
name VARCHAR(255)
)
Lets say you should get all the names in the class, except the one you're searching for. For example, my name is "John Doe", so we search for my name like this:
SELECT
classes_names.`id`
FROM
`classes_names`
INNER JOIN `classes`
ON `status` = 1
WHERE
`name`='John Doe'
In this query, my name will be returned together with my class ID. The thing is, I want the "class members" to be returned exluding myself. So lets say we have this table:
+------+----------+
| id | status |
+------+----------+
| 1 | 1 |
| 2 | 1 |
+------+----------+
+------+----------+---------------+----------+
| id | class_id | name |
+------+----------+--------+-----------------+
| 1 | 1 | John Doe |
| 2 | 1 | Alexandra Fito |
| 3 | 2 | Rico Hasti |
| 4 | 1 | Lady Gaga |
+------+----------+--------------------------+
The query I want to do as "described" with words: SELECT class_names.id WHERE SAME CLASS HAS NAME 'John Doe'.
The returned rows should be ALL members in the class - without the searched name... So the result I should be expecting is:
+------+----------+---------------+----------+
| id | class_id | name |
+------+----------+--------+-----------------+
| 2 | 1 | Alexandra Fito |
| 4 | 1 | Lady Gaga |
+------+----------+--------------------------+
Sooo... Anyone wants to give it a shot? Go for it!