Jump to content

Help with JOIN


dbk

Recommended Posts

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 :wtf: what am I doing wrong?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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!!

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

:-[ 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!!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.