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
https://forums.phpfreaks.com/topic/46851-complex-query-problem/
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
https://forums.phpfreaks.com/topic/46851-complex-query-problem/#findComment-228399
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.