如何从同一个表中获取相关行?

时间:2016-05-23 10:11:28

标签: php mysql sql inner-join

我有一张这样的表:

// mytable
+----+---------+---------+
| id | related |  color  |
+----+---------+---------+
| 1  | 1       | red     |
| 2  | 1       | blue    |
| 3  | 3       | green   |
| 4  | 1       | white   |
| 5  | 3       | brown   |
| 6  | 6       | gray    |
| 7  | 3       | black   |
| 8  | 1       | orange  |
| 9  | 6       | pink    |
+----+---------+---------+

我有id个号码,我需要获得相关id的颜色。

例如:

$id = 4; // I need to get `red`
$id = 5; // I need to get `green`
$id = 6; // I need to get `gray`
$id = 9; // I need to get `gray`

我可以使用JOIN来做到这一点。像这样:

SELECT t2.color FROM mytable t1 JOIN mytable t2 ON t1.related = t2.id WHERE t1.id = :id

我的查询按预期工作..但我不确定使用JOIN这样做是标准的。其实我试图知道有没有更好的方法?或者我的是正常的方式?

3 个答案:

答案 0 :(得分:2)

SELECT t.related FROM mytable t WHERE t.id = :id出了什么问题? JOIN只会检查“相关”列中是否存在实际ID,而不是

答案 1 :(得分:2)

我已经完成了两个不同的查询并解释了它们,希望能给你一些提示。

SQL Fiddle

MySQL 5.6架构

CREATE TABLE mytable
    (`id` int, `related` int, `color` varchar(6))
;

INSERT INTO mytable
    (`id`, `related`, `color`)
VALUES
    (1, 1, 'red'),
    (2, 1, 'blue'),
    (3, 3, 'green'),
    (4, 1, 'white'),
    (5, 3, 'brown'),
    (6, 6, 'gray'),
    (7, 3, 'black'),
    (8, 1, 'orange'),
    (9, 6, 'pink')
;

查询1

SELECT t2.color FROM mytable t1 JOIN mytable t2 ON t1.related = t2.id WHERE t1.id = '4'

<强> Results

| color |
|-------|
|   red |

查询2

explain SELECT t2.color FROM mytable t1 JOIN mytable t2 ON t1.related = t2.id WHERE t1.id = '4'

<强> Results

| id | select_type | table | type | possible_keys |    key | key_len |    ref | rows |                                              Extra |
|----|-------------|-------|------|---------------|--------|---------|--------|------|----------------------------------------------------|
|  1 |      SIMPLE |    t1 |  ALL |        (null) | (null) |  (null) | (null) |    9 |                                        Using where |
|  1 |      SIMPLE |    t2 |  ALL |        (null) | (null) |  (null) | (null) |    9 | Using where; Using join buffer (Block Nested Loop) |

查询3

SELECT t1.color FROM mytable t1 WHERE exists (select 1 from mytable t2 where t1.id =  t2.related and t2.id ='4')

<强> Results

| color |
|-------|
|   red |

查询4

explain SELECT t1.color FROM mytable t1 WHERE exists (select 1 from mytable t2 where t1.id =  t2.related and t2.id ='4')

<强> Results

| id |        select_type | table | type | possible_keys |    key | key_len |    ref | rows |       Extra |
|----|--------------------|-------|------|---------------|--------|---------|--------|------|-------------|
|  1 |            PRIMARY |    t1 |  ALL |        (null) | (null) |  (null) | (null) |    9 | Using where |
|  2 | DEPENDENT SUBQUERY |    t2 |  ALL |        (null) | (null) |  (null) | (null) |    9 | Using where |

答案 2 :(得分:0)

你也可以用简单的方式解决这个问题

select t.color from mytable t where t.id = '$id' (for one value)
select t.color from mytable t where t.id in ('$id1','$id2','$id3','$id4' ) (for multi-values comma separated strings)