0
我使用下表登記系統的工作: 人(姓名等),場(當然爲準),註冊(關聯表PID,CID)幫助與MySQL加入3個表
Person +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | id | int(11) | NO | PRI | NULL | | | name | text | YES | | NULL | | +-------+---------+------+-----+---------+-------+ Example Data: select * from person; +-----+--------+ | id | name | +-----+--------+ | 101 | Graham | | 102 | Lisa | | 103 | John | +-----+--------+ Course +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | id | int(11) | NO | PRI | NULL | | | name | text | YES | | NULL | | | date | date | YES | | NULL | | +-------+---------+------+-----+---------+-------+ select * from course; +----+---------+------------+ | id | name | date | +----+---------+------------+ | 1 | Hip Hop | 2011-06-08 | | 2 | Dancing | 2006-06-23 | | 3 | Running | 2007-07-08 | +----+---------+------------+ Registration +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | id | int(11) | NO | PRI | NULL | | | pid | int(11) | YES | | NULL | | | cid | int(11) | YES | | NULL | | +-------+---------+------+-----+---------+-------+ select * from registration; +----+------+------+ | id | pid | cid | +----+------+------+ | 1 | 101 | 1 | | 2 | 101 | 2 | | 3 | 103 | 2 | +----+------+------+
我想找到在過去兩年內沒有註冊記錄的人員。我試圖根據日期計算來加入表格,但它似乎不能以這種方式工作。這可能與MySQL或我的方法嘗試這與一個查詢錯誤?
查詢我想出了:
select * from (person left join registration on person.id = registration.pid) left join course on course.id = registration.cid AND DATE_FORMAT(`course`.`date`, "%m.%Y") > DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 2 YEAR),"%m.%Y") WHERE registration.id IS NULL; +-----+------+------+------+------+------+------+------+ | id | name | id | pid | cid | id | name | date | +-----+------+------+------+------+------+------+------+ | 102 | Lisa | NULL | NULL | NULL | NULL | NULL | NULL | +-----+------+------+------+------+------+------+------+
應該列出人員102和103,因爲兩個註冊超過2年,新的課程日期,沒有其他記錄可以發現,年齡較大的...
想想子查詢:'SELECT * FROM WHERE人ID NOT IN(SELECT PID FROM .... JOIN ....)' – Erik 2011-06-08 21:59:43