Jump to content

Recommended Posts

Here's my table examples

 

Jobs

job_id

job_name

type_id_1

type_id_2

type_id_3

1

job1

1

1

3

2

job2

3

4

2

3

job3

1

2

3

 

Types

type_id

type

1

type1

2

type2

3

type3

4

type4

 

Now I need to create a query that would give me the result of.

 

job_id

job_name

type1

type2

type3

1

job1

type1

type1

type3

2

job2

type3

type4

type2

3

job3

type1

type2

type3

 

I was hoping the following query might get me what I need.

SELECT jobs.job_id, jobs.job_name, types.type_id AS type1, types.type_id AS type2, types.type_id AS type3
FROM jobs, types
WHERE types.type_id=jobs.type_id_1
AND types.type_id=jobs.type_id_2
ANd types.type_id=jobs.type_id_3
AND jobs.job_id=73

but it's producing a list of empty cells and yes job_id 73 does exist. :-)

 

What am I doing wrong?

Link to comment
https://forums.phpfreaks.com/topic/135500-help-with-another-query/
Share on other sites

SELECT jobs.job_id, jobs.job_name, t1.type_id AS type1, t2.type_id AS type2, t3.type_id AS type3
FROM jobs, types AS t1, types AS t2, types AS t3
WHERE t1.type_id=jobs.type_id_1
AND t2.type_id=jobs.type_id_2
ANd t3.type_id=jobs.type_id_3
AND jobs.job_id=73

 

should work

 

BTW: Your database design is not optimal

It's not normalized

You have three fields in your `Jobs` table to store the same type of information.

In most cases this should be remade into many-to-many relation using one more table.

 

It's hard to be 100% sure without knowing your exact requirements, but I'm like... 93.546% sure ;)

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.