pweb
Guest
LEFT JOIN по одной таблице выводит разные результаты
Делаю такой запрос:
[sql]
SELECT PhorumBody.id, PhorumBody.header, PhorumBody.act, UserCard.login, IFNULL( MAX( t2.dt ) , PhorumBody.dt ) AS maxdt, DATE_FORMAT( IFNULL( MAX( t2.dt ) , PhorumBody.dt ) , '%d/%m/%Y' ) AS last_date, DATE_FORMAT( IFNULL( MAX( t2.dt ) , PhorumBody.dt ) , '%H:%i' ) AS last_time, COUNT( t2.pid ) AS cnt
FROM PhorumBody, UserCard
LEFT JOIN PhorumBody AS t2 ON PhorumBody.id = t2.pid
WHERE PhorumBody.uid = UserCard.id AND PhorumBody.aid = 8 AND PhorumBody.pid = 0
GROUP BY t2.pid
ORDER BY maxdt DESC
[/sql]
поля выводит, но в нем нет записи с PhorumBody.header='test' (она должна быть одна). В этом же запросе ограничиваю условие добавлением PhorumBody.header='test' :
[sql]
SELECT PhorumBody.id, PhorumBody.header, PhorumBody.act, UserCard.login, IFNULL( MAX( t2.dt ) , PhorumBody.dt ) AS maxdt, DATE_FORMAT( IFNULL( MAX( t2.dt ) , PhorumBody.dt ) , '%d/%m/%Y' ) AS last_date, DATE_FORMAT( IFNULL( MAX( t2.dt ) , PhorumBody.dt ) , '%H:%i' ) AS last_time, COUNT( t2.pid ) AS cnt
FROM PhorumBody, UserCard
LEFT JOIN PhorumBody AS t2 ON PhorumBody.id = t2.pid
WHERE PhorumBody.uid = UserCard.id AND PhorumBody.aid = 8 AND PhorumBody.pid = 0 AND PhorumBody.header = 'test'
GROUP BY t2.pid
ORDER BY maxdt DESC
[/sql]
Результат выводит одну запись с PhorumBody.header='test'
объясните пожалуйста, кому не лень, в чем грабли?
Делаю такой запрос:
[sql]
SELECT PhorumBody.id, PhorumBody.header, PhorumBody.act, UserCard.login, IFNULL( MAX( t2.dt ) , PhorumBody.dt ) AS maxdt, DATE_FORMAT( IFNULL( MAX( t2.dt ) , PhorumBody.dt ) , '%d/%m/%Y' ) AS last_date, DATE_FORMAT( IFNULL( MAX( t2.dt ) , PhorumBody.dt ) , '%H:%i' ) AS last_time, COUNT( t2.pid ) AS cnt
FROM PhorumBody, UserCard
LEFT JOIN PhorumBody AS t2 ON PhorumBody.id = t2.pid
WHERE PhorumBody.uid = UserCard.id AND PhorumBody.aid = 8 AND PhorumBody.pid = 0
GROUP BY t2.pid
ORDER BY maxdt DESC
[/sql]
поля выводит, но в нем нет записи с PhorumBody.header='test' (она должна быть одна). В этом же запросе ограничиваю условие добавлением PhorumBody.header='test' :
[sql]
SELECT PhorumBody.id, PhorumBody.header, PhorumBody.act, UserCard.login, IFNULL( MAX( t2.dt ) , PhorumBody.dt ) AS maxdt, DATE_FORMAT( IFNULL( MAX( t2.dt ) , PhorumBody.dt ) , '%d/%m/%Y' ) AS last_date, DATE_FORMAT( IFNULL( MAX( t2.dt ) , PhorumBody.dt ) , '%H:%i' ) AS last_time, COUNT( t2.pid ) AS cnt
FROM PhorumBody, UserCard
LEFT JOIN PhorumBody AS t2 ON PhorumBody.id = t2.pid
WHERE PhorumBody.uid = UserCard.id AND PhorumBody.aid = 8 AND PhorumBody.pid = 0 AND PhorumBody.header = 'test'
GROUP BY t2.pid
ORDER BY maxdt DESC
[/sql]
Результат выводит одну запись с PhorumBody.header='test'
объясните пожалуйста, кому не лень, в чем грабли?