简体   繁体   English

MySQL:选择所有包含ID的行

[英]MySQL: Select all rows containing the ID

I'm trying to write a MySQL query which should select rows from two different tables. 我正在尝试编写一个MySQL查询,该查询应从两个不同的表中选择行。 I have made a different amount of queries but they all take pretty long time before they return the result (> 0.6 seconds). 我进行了不同数量的查询,但是它们都花了很长时间才能返回结果(> 0.6秒)。 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: 例如,我的名字是“ John Doe”,因此我们搜索这样的名字:

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. 在此查询中,我的名字将与我的班级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'. 我想用词“描述”来执行查询: 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". 我应该指出,我不明白指出您的“英语查询”中没有的status位。

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." “选择与John Doe所在班级具有class_id的班级中的任何人,而不是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 为了提高性能,您应该在classes_names.class_id和classes.id上设置索引

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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