简体   繁体   中英

MySQL: Select all rows containing the ID

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!

This should work:

SELECT a.name, 
       b.class_id 
FROM   classes_names a 
       INNER JOIN (SELECT id, 
                          class_id 
                   FROM   classes_names 
                   WHERE  name = 'John Doe') b 
               ON b.class_id = a.class_id 
                  AND a.id <> b.id 

Result

|           NAME | CLASS_ID |
-----------------------------
| Alexandra Fito |        1 |
|      Lady Gaga |        1 |

See the demo

SELECT * FROM classes_names 
WHERE class_id IN (
     SELECT id FROM classes_names 
     WHERE name = "John Doe")
AND name != "John Doe"

I should point out I don't understand point out the status bit as that wasn't in your "English Query".

The best translation into English for this one is:

"Select anyone who is in a class with a class_id of a class that John Doe is in, who isn't John Doe."

SELECT
  classes_names.`id`

FROM
  `classes_names`

Left JOIN `classes`
  (SELECT class_id
FROM classes_names
WHERE name LIKE "John Doe"
) the_class ON classes_names.class_id = the_class.class_id


WHERE
  `status`=1 and
  `name`NOT LIKE'John Doe'
SELECT classes_names.id
FROM classes_names

INNER JOIN (
    SELECT class_id
    FROM classes_names
    WHERE name LIKE "John Doe"
) class_john_doe 
ON (
    classes_names.class_id = class_john_doe.class_id
    AND class_john_doe.id != classes_names.id
)

To improve performance you should set an index on classes_names.class_id and classes.id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM