473,327 Members | 1,936 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,327 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 1036

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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.