473,388 Members | 1,370 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

MySQL Query help

This one has me pretty stumped at the moment. I've tried variations
with MySQL IF() statements but can't quite get it right.

How can I select all of the records of the table 'items' that are
active. Restricted such that if there are items active for the
subcategory and the parent category *only* the items of the
subcategory get returned and if there are only items active for the
parent category they get returned. The parent category will always
have active items, but the subcategory may not.

Given the following schema (subset of the the real tables naturally):

#
# Table structure for table `categories`
#

CREATE TABLE `categories` (
`cat_id` int(11) NOT NULL auto_increment,
`parent_id` int(11) NOT NULL default '0',
`cat_name` varchar(10) NOT NULL default '',
PRIMARY KEY (`cat_id`)
) TYPE=MyISAM AUTO_INCREMENT=3 ;

#
# Dumping data for table `categories`
#

INSERT INTO `categories` VALUES (1, 0, 'parent');
INSERT INTO `categories` VALUES (2, 1, 'child');

# --------------------------------------------------------

#
# Table structure for table `items`
#

CREATE TABLE `items` (
`item_id` int(11) NOT NULL auto_increment,
`cat_id` int(11) NOT NULL default '0',
`item_text` varchar(10) NOT NULL default '',
`active` int(11) NOT NULL default '1',
PRIMARY KEY (`item_id`),
KEY `category_id` (`cat_id`)
) TYPE=MyISAM AUTO_INCREMENT=14 ;

#
# Dumping data for table `items`
#

INSERT INTO `items` VALUES (1, 1, 'parent it', 1);
INSERT INTO `items` VALUES (2, 1, 'parent it', 1);
INSERT INTO `items` VALUES (3, 1, 'parent it', 1);
INSERT INTO `items` VALUES (4, 1, 'parent it', 1);
INSERT INTO `items` VALUES (5, 1, 'parent it', 1);
INSERT INTO `items` VALUES (10, 2, 'child_it', 1);
INSERT INTO `items` VALUES (11, 2, 'child_it', 1);
INSERT INTO `items` VALUES (12, 2, 'child_it', 1);
INSERT INTO `items` VALUES (13, 2, 'child_it', 1);

Any help would be appreciated!
Jul 19 '05 #1
0 1275

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Brian | last post by:
Hello I am using version 4.0.12-nt of MySQL and when I hit the enter key rapidly I can't connect to the database. The result is a message is returned to me from mysql that says I can't connect...
51
by: w_curtis | last post by:
I'm an Access user, and I'm trying to learn MySQL and then PHP so I can make some web databases. But it just isn't clicking. I've followed some tutorials, and picked up a book, but just getting...
0
by: Mike Chirico | last post by:
Interesting Things to Know about MySQL Mike Chirico (mchirico@users.sourceforge.net) Copyright (GPU Free Documentation License) 2004 Last Updated: Mon Jun 7 10:37:28 EDT 2004 The latest...
2
by: pratchaya | last post by:
This is my sample error in my MySQL Log New value of fp=(nil) failed sanity check, terminating stack trace! Please read http://www.mysql.com/doc/en/Using_stack_trace.html and follow instructions...
39
by: Mairhtin O'Feannag | last post by:
Hello, I have a client (customer) who asked the question : "Why would I buy and use UDB, when MySql is free?" I had to say I was stunned. I have no experience with MySql, so I was left sort...
1
by: jlee | last post by:
I'm pretty much a newbie on mysql, and I need some help. I am running mysql Ver 12.22 Distrib 4.0.24, for portbld-freebsd5.4 (i386) on a server hosting an active website. The site's developer...
1
by: Good Man | last post by:
Hi there I've noticed some very weird things happening with my current MySQL setup on my XP Laptop, a development machine. For a while, I have been trying to get the MySQL cache to work....
8
by: Fred | last post by:
Hello, Our website is currently developed in ASP/Mysql 4. The dedicated servers on which it is currently hosted arrive at saturation. Here is their configuration: - 1 server PIV 2,8Ghz 1GB...
3
by: Juan Antonio Villa | last post by:
Hello, I'm having a problem replicating a simple database using the binary log replication, here is the problem: When the master sends an update to the slave, an example update reads as follows:...
10
by: Caffeneide | last post by:
I'm using a php script which performs three xml queries to other three servers to retrieve a set of ids and after I do a query to mysql of the kind SELECT * FROM table WHERE id IN ('set of ids');...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.