0

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!

4

4 に答える 4

2

これはうまくいくはずです:

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 

結果

| | 名前 | CLASS_ID |
------------------------------
| | アレクサンドラ・フィトー | 1 |
| | レディー・ガガ | 1 |

デモを見る

于 2013-02-28T21:58:23.607 に答える
1
SELECT * FROM classes_names 
WHERE class_id IN (
     SELECT id FROM classes_names 
     WHERE name = "John Doe")
AND name != "John Doe"

statusあなたの「英語の質問」にはなかったので、私は少し指摘するのが理解できないことを指摘する必要があります。

これに最適な英語への翻訳は次のとおりです。

「JohnDoeが所属しているクラスclass_idの、JohnDoeではないクラスに所属している人を選択してください。」

于 2013-02-28T21:57:48.077 に答える
0
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'
于 2013-02-28T21:55:01.807 に答える
0
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
)

パフォーマンスを向上させるには、classes_names.class_idとclasses.idにインデックスを設定する必要があります

于 2013-02-28T21:55:09.403 に答える