Jump to content

complex query problem


pouncer

Recommended Posts

I have 2 tables group_categories and categories

 

group_categories

lion

tiger

 

categories contains the following animals:

cat

dog

lion

tiger

zebra

 

basically i want to echo all the animals in 'categories' that are NOT in 'group_categories'

so it would be cat,dog, zebra

 

anyone can help me?

Link to comment
Share on other sites

I think you should make some changes to your database.

 

Let's try this:

 

Make three tables in your database:

1. tbl_animals

id animal_name category_id

 

2. tbl_categories

id category_name

 

Than you could provide some combo for users to choose category for animals. When that happend save ID of that category to table tbl_animals.category_id.

 

So later when you want to populate some combo with all animals who are without catergory_id (that means you hadn't set category for those animals) you can make query:

SELECT * FROM tbl_animals WHERE category_id = NULL

.

 

If you want query database to get results of all animals and their categories you can use JOIN method:

SELECT tbl_animals.*, tbl_categories.category_name FROM tbl_animals LEFT JOIN tbl_categories ON tbl_animals.category_id = tbl_categories.id

 

I'm not sure if that could be solution for you. It independents of what you want to do exactly.

 

But I hope this was helpfull for you.

 

Cheers ;)

 

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.