我不确定是否必须制作数据透视表或某些子查询才能获得所需的结果,但这是我的表格:
asset_id id title title_type 34 1 episode 1 5 34 2 TNS 4 34 3 WXPR 3 35 4 episode 57 5 35 5 BLSH 4 35 6 WXRE 3 36 7 episode 56 5 36 8 BLSH 4 36 9 WXRE 3
但这就是我想要的。每个title_type在它自己的列中,WHERE标题与title_type 4 = BLSH。像这样:
asset id title_type 5 title_type 4 title_type 3 35 episode 56 BLSH WXRE 36 episode 57 BLSH WXRE
答案 0 :(得分:1)
真正的解决方法当然是改变你的数据库结构; - )
但是,您可以使用JOIN来获取所需的数据。在我的例子中,没有涉及子选择:
SELECT
o.asset_id,
/* select the data from the self-joins */
`5`.title AS title_type_5,
`4`.title AS title_type_4,
`3`.title AS title_type_3
FROM
foo o
/* use self-joins to combine data of multiple rows in a single row */
LEFT JOIN foo `3` ON `3`.title_type = 3 AND `3`.asset_id = o.asset_id
LEFT JOIN foo `4` ON `4`.title_type = 4 AND `4`.asset_id = o.asset_id
LEFT JOIN foo `5` ON `5`.title_type = 5 AND `5`.asset_id = o.asset_id
WHERE
o.title_type = "4"
AND o.title = "BLSH"