如何加入表格?

时间:2013-03-26 09:25:33

标签: mysql sql

我有三张桌子:

部分:

+-------+------------------+------+-----+---------+----------------+
| Field | Type             | Null | Key | Default | Extra          |
+-------+------------------+------+-----+---------+----------------+
| id    | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
+-------+------------------+------+-----+---------+----------------+
+----+
| id |
+----+
|  1 |
|  2 |
|  3 |
+----+

parts_partid:

+-----------+------------------+------+-----+---------+-------+
| Field     | Type             | Null | Key | Default | Extra |
+-----------+------------------+------+-----+---------+-------+
| part_id   | int(10) unsigned | NO   | PRI | NULL    |       |
| partid_id | int(10) unsigned | NO   | PRI | NULL    |       |
+-----------+------------------+------+-----+---------+-------+
+---------+-----------+
| part_id | partid_id |
+---------+-----------+
|       1 |         1 |
|       1 |         2 |
|       1 |         3 |
|       2 |         1 |
|       2 |         4 |
|       2 |         5 |
|       3 |         6 |
|       3 |         7 |
|       3 |         8 |
+---------+-----------+

PARTID:

+-------+------------------+------+-----+---------+----------------+
| Field | Type             | Null | Key | Default | Extra          |
+-------+------------------+------+-----+---------+----------------+
| id    | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| key   | varchar(50)      | NO   | MUL | NULL    |                |
| value | varchar(50)      | NO   |     | NULL    |                |
+-------+------------------+------+-----+---------+----------------+
+----+-----+-------+
| id | key | value |
+----+-----+-------+
|  1 | l   | foo   |
|  2 | p   | bar   |
|  3 | s   | baz   |
|  4 | l   | 3     |
|  5 | p   | 5     |
|  6 | s   | r     |
|  7 | l   | rr    |
|  8 | l   | rrr   |
+----+-----+-------+

基本上每个部分都包含任意的part-id。例如第1部分有l ='foo',p ='bar'和s ='baz'。

如果我想搜索所有l ='foo'的部分,我会使用此连接

SELECT p.id FROM parts AS p
INNER JOIN parts_partid ppi ON ppi.part_id = p.id
INNER JOIN partid pi ON ppi.partid_id = pi.id
WHERE pi.key = 'l' AND pi.value = 'foo';

这给了我#1和#2的部分。

现在我想搜索所有l ='foo' p ='5'的部分(这应该只产生部分#2)。我怎么能这样做?

2 个答案:

答案 0 :(得分:0)

试试这个......

SELECT p.id FROM parts AS p
INNER JOIN parts_partid ppi ON ppi.part_id = p.id
INNER JOIN partid pi ON ppi.partid_id = pi.id
WHERE (pi.key = 'l' AND pi.value = 'foo')
or    (pi.key ='p' and pi.value = '5')

答案 1 :(得分:0)

试试这个:

SELECT id
FROM
(
  SELECT 
    p.id,
    MAX(CASE WHEN pi.Key = 'l' THEN value END) AS l,
    MAX(CASE WHEN pi.Key = 'p' THEN value END) AS p,
    MAX(CASE WHEN pi.Key = 's' THEN value END) AS s
  FROM parts AS p
  INNER JOIN parts_partid ppi ON ppi.part_id = p.id
  INNER JOIN partid pi ON ppi.partid_id = pi.id
  GROUP BY p.id
) AS t
WHERE l = 'foo' 
  AND p = 5;

SQL Fiddle Demo

这只会给你第2部分:

<强> Results

| ID |
------
|  2 |

此查询背后的想法是子查询,它将将键的值转换为列;它会为每个id提供那些键作为列标题:

| ID |   L |      P |      S |
------------------------------
|  1 | foo |    bar |    baz |
|  2 | foo |      5 | (null) |
|  3 | rrr | (null) |      r |

然后在外部查询中,您可以过滤这些列的值。