dbk Posted June 17, 2010 Share Posted June 17, 2010 Hi I need some help with a query that don't work: SELECT * FROM contact JOIN project_contact USING (contact_id) WHERE project_id = 1301 I have two tables - "contact" and "project_contact". "contact" contains some information about people. "project_contact" contains information about wich projects a contact is linked to. Both tables has a column "contact_id". But the "project_contact" table has also a column "project_id" that I want to sort the contacts with!!! If you understand the explanation what am I doing wrong? Quote Link to comment https://forums.phpfreaks.com/topic/205102-help-with-join/ Share on other sites More sharing options...
Psycho Posted June 17, 2010 Share Posted June 17, 2010 Well, you state you want to use the project_id to "sort" the records, but your query is currently filtering the records. Other than that, I see no obvious problems. Although, if there are other columsn with duplicate names between the tables that would cause an error. You don't state if you are getting any errors. If so, please post them. If you really do want to sort instead of filter the query would look something like this: SELECT * FROM contact JOIN project_contact USING (contact_id) ORDER BY project_id Quote Link to comment https://forums.phpfreaks.com/topic/205102-help-with-join/#findComment-1073646 Share on other sites More sharing options...
dbk Posted June 18, 2010 Author Share Posted June 18, 2010 Hi mjdamato Sorry for my english.. What I want is to filter using the "project_id" ! each contact can be linked to many projects and thats why I made the "project_contact" table that only contain a primary key with autoincrease, the contact_id and the project_id. Hope it makes sense! By the way.. I don't get any error using: SELECT * FROM contact JOIN project_contact USING (contact_id) WHERE project_id = 1301 The filter just don't work!! Quote Link to comment https://forums.phpfreaks.com/topic/205102-help-with-join/#findComment-1073786 Share on other sites More sharing options...
Psycho Posted June 18, 2010 Share Posted June 18, 2010 Well, I can't determine what is wrogn without seeing the data since the queery looks fine. Try posting the table structure for the two tables and provide a few sample records that are being returned from the query which you feel shouldn't be. Quote Link to comment https://forums.phpfreaks.com/topic/205102-help-with-join/#findComment-1073956 Share on other sites More sharing options...
dbk Posted June 18, 2010 Author Share Posted June 18, 2010 Hi mjdamato I just found out that the error was in some other code (php). The query returns the correct data! As I once was told - ALLWAYS TRY THE QUERY IN CMD!!! I'm very sorry for wasting your time!! Quote Link to comment https://forums.phpfreaks.com/topic/205102-help-with-join/#findComment-1073974 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.