otixz Posted April 26, 2008 Share Posted April 26, 2008 Hi I have a query like this: select total , (select total from tblinquiries where inq_date='22/04/2008' and course='Comp Mgt') as Total2 from tblinquiries where inq_date='21/04/2008' and course='Comp Mgt' if the column total and total2 has result, the database will show the result. however, if either the total or total2 has a null value, it will not show the result. What I want is at least there will be a number 0 in the column which has a null value. example total | total2 0 | 58 or total | total2 58 | 0 how can I have this result? so that I can show it in a table in PHP? Link to comment https://forums.phpfreaks.com/topic/103009-solved-mysql-query-problem/ Share on other sites More sharing options...
Gamic Posted April 26, 2008 Share Posted April 26, 2008 I'm not sure if this is exactly what you want, but I think it'll help get you some of the way there. select o.total, t.total as Total2 from tblinquiries as o left join tblinquiries as t where o.inq_date = '21/04/2008' and t.inq_date = '22/04/2008' and t.course = 'Comp Mgt' and t.course = o.course; Link to comment https://forums.phpfreaks.com/topic/103009-solved-mysql-query-problem/#findComment-527669 Share on other sites More sharing options...
otixz Posted April 26, 2008 Author Share Posted April 26, 2008 Thank you for your help...however i have a mysql querry error.... Error Code : 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where o.inq_date = '21/04/2008' and t.inq_date = '22/04/2008' and t.course ' at line 3 (0 ms taken) Link to comment https://forums.phpfreaks.com/topic/103009-solved-mysql-query-problem/#findComment-527671 Share on other sites More sharing options...
Gamic Posted April 26, 2008 Share Posted April 26, 2008 Just to figure out where I went wrong I did this: create table tblinquiries( inq_date char(10), total int, course varchar(30)); insert into tblinquiries values('21/04/2008',58,'Comp Mgt'); insert into tblinquiries values('22/04/2008',null,'Comp Mgt'); This query seems to work: select ifnull(o.total,0) as total, ifnull(t.total,0) as Total2 from tblinquiries as o left join tblinquiries as t on t.course=o.course where o.inq_date = '21/04/2008' and t.inq_date = '22/04/2008' and t.course = 'Comp Mgt'; output: total | total2 58| 0 Link to comment https://forums.phpfreaks.com/topic/103009-solved-mysql-query-problem/#findComment-527679 Share on other sites More sharing options...
otixz Posted April 26, 2008 Author Share Posted April 26, 2008 Thank you! that answered my question... But I have a harder problem Let's say that there is no 04/21/2008 date... can it still produce the same 2 columned result? total | total2 0 | 25 // example there is no date of 04/21/2008 for the course 'Comp Mgt' Thank you very much Link to comment https://forums.phpfreaks.com/topic/103009-solved-mysql-query-problem/#findComment-527686 Share on other sites More sharing options...
Gamic Posted April 26, 2008 Share Posted April 26, 2008 select ifnull( (select o.total from tblinquiries o where o.inq_date='21/04/2008' and o.course='Comp Mgt') ,0) as total, ifnull( (select t.total from tblinquiries t where t.inq_date='23/03/2008' and t.course='Comp Mgt') ,0) as Total2; +-------+--------+ | total | Total2 | +-------+--------+ | 58 | 0 | +-------+--------+ 1 row in set (0.00 sec) mysql> select * from tblinquiries; +------------+-------+----------+ | inq_date | total | course | +------------+-------+----------+ | 21/04/2008 | 58 | Comp Mgt | | 22/04/2008 | NULL | Comp Mgt | +------------+-------+----------+ 2 rows in set (0.00 sec) Link to comment https://forums.phpfreaks.com/topic/103009-solved-mysql-query-problem/#findComment-527691 Share on other sites More sharing options...
otixz Posted April 26, 2008 Author Share Posted April 26, 2008 Wow! that really works! thank you very much! I really hope I can learn more from you guys! Thanks! Link to comment https://forums.phpfreaks.com/topic/103009-solved-mysql-query-problem/#findComment-527697 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.