0

所以,我正在尝试查询多个表。

这是我到目前为止所拥有的:

SELECT * FROM patientdetails  
WHERE DATEDIFF( CURRENT_DATE, DOB ) /365 <18 
and Gender='Male'  
AND RAMQ in 
(
   SELECT `RAMQ` 
   FROM `pathology` 
   WHERE `Result`='positive'
)

这工作得很好,但我需要将日期与另一个表中的日期字段进行比较,我尝试了许多不同的解决方案。

解决方案1:

SELECT * FROM patientdetails  
WHERE DATEDIFF( pathology.Date, DOB ) /365 <18 
and Gender='Male'  
AND RAMQ in 
(
   SELECT `RAMQ`  
   FROM `pathology` 
   WHERE `Result`='positive'
)   

解决方案2:

SELECT * FROM patientdetails p, pathology pp  
WHERE DATEDIFF( pp.Date, p.DOB ) /365 <18 
and p.Gender='Male'  
AND p.RAMQ in 
(
   SELECT `RAMQ` 
   FROM `pathology` 
   WHERE `Result`='positive'
)

这些似乎都不起作用,而且非常令人沮丧。如果有人可以为我提供一些指导,将不胜感激。

4

2 回答 2

1

JOIN他们这样:

SELECT d.* 
FROM patientdetails  AS d
INNER JOIN pathology AS p ON d.RAMQ = p.RAMQ
WHERE DATEDIFF(p.Date, d.DOB ) /365 < 18 
  AND d.Gender='Male' 
于 2013-05-17T15:01:07.817 回答
0

我们可以从这个开始吗...

SELECT * 
  FROM patientdetails pd 
  JOIN pathology g
    ON g.RAMQ = pd.RAMQ 
   AND g.Result = 'positive'
 WHERE pd.DOB > CURDATE() - INTERVAL 18 YEAR 
   AND pd.Gender='Male';  

...?

于 2013-05-17T15:11:25.337 回答