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 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 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. 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? 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) 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 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. 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? Link to comment https://forums.phpfreaks.com/topic/224528-incorrect-syntax/#findComment-1161034 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.