MySQL:加入一个表给自己

时间:2011-10-02 18:59:20

标签: mysql join

我有一个首选项表,名为“txp_prefs”。我想将多个首选项返回到一行;我更喜欢这个简单串联的原因是我在textpattern中使用了一个可以处理单行的插件。

以下是我的测试数据:

------------------------------------------------
|Id | event   | name             |value        |
------------------------------------------------
| 1 | season  | season_start     | 12/10/2011  |
-----------------------------------------------
| 2 | season  | season_end       | 29/10/2011  |
------------------------------------------------
| 3 | season  | season_countdown | 7           |
------------------------------------------------
| 4 | another | test1            | result1     |
------------------------------------------------
| 3 |         | test2            | result2     |
------------------------------------------------

我想得到的最终结果是:

----------------------------------------------------------
|event    | season_start | season_end | season_countdown |
----------------------------------------------------------
|season   | 12/10/2011   | 29/10/2011 | 7                |
----------------------------------------------------------

我可以(显然)创建单独的查询以独立获取每个结果;例如

SELECT t1.event, t1.val AS season_start FROM txp_prefs t1 WHERE t1.event="season" AND t1.name="season_start" (to get the season_start)
SELECT t2.event, t2.val AS season_end FROM txp_prefs t2 WHERE t2.event="season" AND t2.name="season_end" (to get the season_end)

但是当我尝试将两者结合在一起时,我会遇到错误,例如:

SELECT t1.event, t1.val AS season_start FROM txp_prefs t1 WHERE t1.event="season" AND t1.name="season_start"
LEFT JOIN 
(SELECT t2.event, t2.val AS season_end FROM txp_prefs t2 WHERE t2.event="season" AND t2.name="season_end") t3
ON t1.event=t3.event

错误消息表明它与连接有关(我猜想无论如何 - 这两个查询都有效。

有什么想法吗?我最近想通过将不同的表连接在一起,所以我假设可以将表连接到自身。

3 个答案:

答案 0 :(得分:1)

根据给定的结构,您可以使用

SELECT 
     MAX(CASE WHEN name = 'season_start' THEN value END) AS season_start,
     MAX(CASE WHEN name = 'season_end' THEN value END) AS season_end,
     MAX(CASE WHEN name = 'season_countdown' THEN value END) AS season_countdown
FROM txp_prefs
WHERE event='season'

答案 1 :(得分:0)

选择您正在寻找的是:

SELECT distinct
          t0.event, 
          t1.val AS season_start , 
          t2.val as seasson_end, 
          t3.val as season_countdown
    FROM 
       txp_prefs t0 
          left outer join
       txp_prefs t1 
          on ( t1.event=t0.event AND t1.name="season_start" )
          left outer join
       txp_prefs t2
          on ( t2.event=t0.event AND t2.name="season_end" )
          left outer join
       txp_prefs t3
          on ( t3.event=t0.event AND t3.name="season_countdown" )
    WHERE 
       t0.event="season"

(只获得一行的标准方法是'distintc'保留字。另一种解决方案是追加'LIMIT 1'来查询,但这是MySQL依赖的)

您确定您的数据库是否正确规范化了吗?

见到你。

答案 2 :(得分:0)

你可以通过枢轴来做到这一点。 Asper我以前的项目我在跟踪查询希望对您有用。

我的桌面交易有以下字段

 NAME VARCHAR2(10)
 branch_code NUMBER(4)
 Ruppes NUMBER(4)


SQL> select * from transaction;

NAME branch_code Ruppes
---------- ---------- ----------
Hemang 2602 1000
Hemang 2603 2000
Hemang 2400 3000
Yash 2602 1500
Yash 2603 1200
Yash 2400 1340
Krupesh 2602 1250
Krupesh 2603 2323
Krupesh 2400 8700

选择了9行。

现在转动。

SQL> select branch_code, 
  2 max( decode( name, 'Hemang', Ruppes, null ) ) "Hemang",
  3 max( decode( name, 'Yash', Ruppes, null ) ) "Yash",
  4 max( decode( name, 'Krupesh', Ruppes, null ) ) "Krupesh"
  5 from
  6 (
  7 select name, branch_code, Ruppes
  8 from transaction
  9 )
 10 group by branch_code ;

      branch_code Hemang Yash Krupesh
---------- ---------- ---------- ----------
      2602 1000 1500 1250
      2603 2000 1200 2323
      2400 3000 1340 8700