目前我不知道如何为SQL Server编写这个SQL语句。
我有两张桌子:
客户:
ID | Name | Prename
----------------------
1 Miller Thomas
....
步骤:
ID | FK_customer | step | created
------------------------------------
1 1 A 2010-02-03
2 1 B 2011-09-12
....
如果我尝试加入,我就明白了:
Name | A_date | B_date
----------------------------------
Miller 2010-02-03 NULL
Miller NULL 2011-09-12
我想要的是:
Name | A_date | B_date
---------------------------------
Miller 2010-02-03 2011-09-12
有人能给我看灯吗?
答案 0 :(得分:8)
您可以使用条件聚合:
if(mysql_num_rows($getphotoToGuess ) > 0){
// do something
}
答案 1 :(得分:1)
SELECT c.Name, A.A_date, B.B_date
FROM Customer c
CROSS APPLY
(
SELECT MAX(created) AS A_date
FROM steps s
WHERE s.FK_customer = c.Id
AND s.step = 'A'
)AS A
CROSS APPLY
(
SELECT MAX(created) AS B_date
FROM steps s
WHERE s.FK_customer = c.Id
AND s.step = 'B'
)B