[英]combining two select queries from the same table
I need to do something like this: 我需要做这样的事情:
id tag status user trial Value (other columns)...
1 A Pass peter first 0
2 A Pass peter second 1
3 A Fail peter third 3
4 B Pass peter first 4
5 B Pass peter second 5
6 B Pass peter third 6
select the rows that tag equal A and status equal to Pass and find the same value for other tag ex:B 选择标记等于A并且状态等于Pass的行,并为其他标记ex:B找到相同的值
id tag status user trial Value_tag_A Value_tag_B (other columns)...
1 A Pass peter first O 4
2 A Pass peter second 1 5
I can do some processing using php to get this result, but i'm wondering if i can do it directly using sql 我可以使用php进行一些处理以获得此结果,但我想知道是否可以直接使用sql来完成
I've tried numerous variations and can't seem to get close to the result. 我已经尝试了多种变体,但似乎无法接近结果。
Solution: http://sqlfiddle.com/#!9/e9068d/17 解决方案: http : //sqlfiddle.com/#!9/e9068d/17
I don't know why in the rows where tag=A
also have Value_tag_B
. 我不知道为什么在
tag=A
的行中也有Value_tag_B
。 I will ignore this and maybe the following query is an approach. 我将忽略这一点,也许以下查询是一种方法。
SELECT DISTINCT y.status, y.`user`, y.trial,
(SELECT Value FROM toto WHERE y.`user` = `user` and y.trial = trial and tag = 'A' ) AS Value_tag_A,
(SELECT Value FROM toto WHERE y.`user` = `user` and y.trial = trial and tag = 'B' ) AS Value_tag_B
FROM toto y
WHERE y.trial NOT IN (SELECT DISTINCT trial FROM toto WHERE `status` <> 'Pass')
The code has been modified. 该代码已被修改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.