Siggles Posted January 28, 2008 Share Posted January 28, 2008 In my Dbase we have 2 tables - Predictions and Fixtures. They are linked by colum 'id'. I am able to show all fixtures where the user has predicted but I want to show all fixtures where the user hasn't. To show where they all HAVE (and this works)... SELECT fixtures.dateplayed, fixtures.opponent, fixtures.resultus, fixtures.resultthem, predictions.resultmfc, predictions.resultother, predictions.score FROM predictions Right Join fixtures ON fixtures.id = predictions.id AND predictions.username = '$session->username' ORDER BY fixtures.dateplayed DESC So I tried... SELECT fixtures.dateplayed, fixtures.opponent FROM predictions Right Join fixtures ON fixtures.id != predictions.id AND predictions.username = '$session->username' ORDER BY fixtures.dateplayed DESC and that didn't work. It just showed the prdedictions over about 10 times. I have tried IS NULL but if a prediction has not been made for a fixture then a row doesn't exist at all in predictions table. Is there a 'does not exist at all' SQL command. If you need mroe info let me know. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/88192-solved-displaying-rows-of-tables-where-id-does-not-exist-in-other-table/ Share on other sites More sharing options...
rhodesa Posted January 28, 2008 Share Posted January 28, 2008 Try SELECT dateplayed, opponent FROM fixtures WHERE id NOT IN (SELECT id FROM predictions) AND predictions.username = '$session->username' ORDER BY dateplayed DESC Quote Link to comment https://forums.phpfreaks.com/topic/88192-solved-displaying-rows-of-tables-where-id-does-not-exist-in-other-table/#findComment-451243 Share on other sites More sharing options...
Siggles Posted January 28, 2008 Author Share Posted January 28, 2008 Thank you for your help. I sort of see what you are getting at, however SQL errors... MySQL said: #1109 - Unknown table 'predictions' in where clause There is definitely a predictions table? It doesn't like the bit in brackets though. Quote Link to comment https://forums.phpfreaks.com/topic/88192-solved-displaying-rows-of-tables-where-id-does-not-exist-in-other-table/#findComment-451267 Share on other sites More sharing options...
rhodesa Posted January 28, 2008 Share Posted January 28, 2008 Oops...try this: SELECT dateplayed, opponent FROM fixtures WHERE id NOT IN (SELECT id FROM predictions WHERE username = '$session->username') ORDER BY dateplayed DESC Quote Link to comment https://forums.phpfreaks.com/topic/88192-solved-displaying-rows-of-tables-where-id-does-not-exist-in-other-table/#findComment-451271 Share on other sites More sharing options...
Siggles Posted January 28, 2008 Author Share Posted January 28, 2008 Oops...try this: SELECT dateplayed, opponent FROM fixtures WHERE id NOT IN (SELECT id FROM predictions WHERE username = '$session->username') ORDER BY dateplayed DESC You got it! Thank you very much! :-D Quote Link to comment https://forums.phpfreaks.com/topic/88192-solved-displaying-rows-of-tables-where-id-does-not-exist-in-other-table/#findComment-451281 Share on other sites More sharing options...
Siggles Posted February 4, 2008 Author Share Posted February 4, 2008 Hi, Now I want to show all fixtures where the user has made a prediction. I tried changing NOT IN for IN but that doesn't work. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/88192-solved-displaying-rows-of-tables-where-id-does-not-exist-in-other-table/#findComment-457702 Share on other sites More sharing options...
rhodesa Posted February 4, 2008 Share Posted February 4, 2008 I am able to show all fixtures where the user has predicted but I want to show all fixtures where the user hasn't. To show where they all HAVE (and this works)... SELECT fixtures.dateplayed, fixtures.opponent, fixtures.resultus, fixtures.resultthem, predictions.resultmfc, predictions.resultother, predictions.score FROM predictions Right Join fixtures ON fixtures.id = predictions.id AND predictions.username = '$session->username' ORDER BY fixtures.dateplayed DESC You said you had that working already? You JOIN here looks good. Quote Link to comment https://forums.phpfreaks.com/topic/88192-solved-displaying-rows-of-tables-where-id-does-not-exist-in-other-table/#findComment-457704 Share on other sites More sharing options...
Siggles Posted February 4, 2008 Author Share Posted February 4, 2008 Sorry, worked it out. Change NOT IN for IN but make < to > Derr! :-) Quote Link to comment https://forums.phpfreaks.com/topic/88192-solved-displaying-rows-of-tables-where-id-does-not-exist-in-other-table/#findComment-457708 Share on other sites More sharing options...
Siggles Posted February 4, 2008 Author Share Posted February 4, 2008 In fact no, just change NOT IN to IN. It was just that there were no fixtures without scores that were later than today's date. Quote Link to comment https://forums.phpfreaks.com/topic/88192-solved-displaying-rows-of-tables-where-id-does-not-exist-in-other-table/#findComment-457734 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.