Destramic Posted January 15, 2011 Share Posted January 15, 2011 im having a probelm with this query on a one to one relationship...anyone know why please? SELECT n.news_id, u.username, n.news, n.date, n.time FROM news n LEFT JOIN users u ON news u.user_id = n.user_id ORDER BY news ASC Quote Link to comment https://forums.phpfreaks.com/topic/224528-incorrect-syntax/ Share on other sites More sharing options...
mikosiko Posted January 16, 2011 Share Posted January 16, 2011 LEFT JOIN users u ON news u.user_id = n.user_id ORDER BY news check what is marked in red Quote Link to comment https://forums.phpfreaks.com/topic/224528-incorrect-syntax/#findComment-1160020 Share on other sites More sharing options...
Muddy_Funster Posted January 16, 2011 Share Posted January 16, 2011 Addressing the code highlighted by mikosiko should get your query to run. Just a note on your join though: right and left joins are used for one to many / many to one relationships, inner and natural joins are used for one to one relationships. Quote Link to comment https://forums.phpfreaks.com/topic/224528-incorrect-syntax/#findComment-1160322 Share on other sites More sharing options...
Destramic Posted January 17, 2011 Author Share Posted January 17, 2011 ive got it working now guy...what would you mean by a natural join please?...could you create an example out of my sql above? Quote Link to comment https://forums.phpfreaks.com/topic/224528-incorrect-syntax/#findComment-1160841 Share on other sites More sharing options...
mikosiko Posted January 17, 2011 Share Posted January 17, 2011 worth a read as a general guidance: http://en.wikipedia.org/wiki/Join_(SQL) Quote Link to comment https://forums.phpfreaks.com/topic/224528-incorrect-syntax/#findComment-1160869 Share on other sites More sharing options...
Destramic Posted January 17, 2011 Author Share Posted January 17, 2011 thanks alot this works great...also what would be the best way to count my news_comments table by news_id in this query below please? SELECT news_id, user_id, news, date, time, user_id, session_id, username, password, email, user_access FROM news INNER JOIN users USING (user_id) ORDER BY news ASC Quote Link to comment https://forums.phpfreaks.com/topic/224528-incorrect-syntax/#findComment-1160929 Share on other sites More sharing options...
Muddy_Funster Posted January 17, 2011 Share Posted January 17, 2011 I personaly would make it a different query. SELECT SUM(news) AS TotalPosts FROM news GROUP BY news_id also, your seem to be selecting user_id twice, which you don't need to do. Quote Link to comment https://forums.phpfreaks.com/topic/224528-incorrect-syntax/#findComment-1160949 Share on other sites More sharing options...
Destramic Posted January 17, 2011 Author Share Posted January 17, 2011 is there away to do it as a one to many relationship or add to the query i already have? Quote Link to comment https://forums.phpfreaks.com/topic/224528-incorrect-syntax/#findComment-1161034 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.