Computer Webmaster Gaming Console Graphics Forum

Welcome to the Computer Webmaster Gaming Console Graphics Forum forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.

MK PitStop Main Earn $25 Earn Money Posting Extras Members Blogs Image Hosting User Pages
Go Back   Computer Webmaster Gaming Console Graphics Forum > Webmaster Forum > Website Coding > Database
Register FAQ/Rules Become A V.I.P. Member Search Today's Posts Mark Forums Read

Database Database problems or need to ask a question? maybe something to do with sql injections or a database software question. Database topics cover MySQL, PostgreSQL, Oracle, SQL Server or anything else related to databases.

Google
Closed Thread
 
LinkBack Thread Tools Display Modes
Old 07-01-2007, 9:34 PM   #1
Sandy.Pittendrigh@gmail.com
 
Sandy.Pittendrigh@gmail.com's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default nested selects on mysql_4.0

I'm stuck with a mysql 4.0 server--which does not allow
nested selects. I want to attach arbitrary 'attributes'
to a 'widgets' table and then query for all widgets
that have two or more specified attributes.

create table widget
(
widget_id int not null auto_increment primary key,
widget_name varchar(48)
);

create table attribute
(
attribute_id int not null auto_increment primary key,
attribute_name varchar(48)
);

## attach attributes to the widget table
create table attribute_list
(
widget_id int not null references widget(widget_id),
attribute_id int not null references attribute(attribute_id),
);


So, each widget can have multiple attributes attached via
the attribute_list "junction link" table.

I have to use mysql_4.0 which does not allow nested selects.
How can I query for all widgets that have, for instance, both the "red"
and "big"
attribute attached, via the attribute_list table?

 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Advertisements
Old 07-01-2007, 9:34 PM   #2
strawberry
 
strawberry's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default nested selects on mysql_4.0

There must be very obvious and much simpler solution - but I'm stumped
as to what it is.

Anyway, I think this works: (6 and 9 represent 'big' and 'red' in your
example)

SELECT widget_id,GROUP_CONCAT(attribute_id),COUNT(attribu te_id) AS cnt
FROM widget_attribute WHERE attribute_id IN (6,
9) GROUP BY widget_id HAVING cnt > 1;

 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Old 07-01-2007, 9:34 PM   #3
Bill Karwin
 
Bill Karwin's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default nested selects on mysql_4.0

Sandy.Pittendrigh@gmail.com wrote:
> How can I query for all widgets that have, for instance, both the "red"
> and "big" attribute attached, via the attribute_list table?


Here's another solution besides the one posted by strawberry:

SELECT w.*
FROM widget AS w
JOIN attribute_list AS l ON w.widget_id = l.widget_id
JOIN attribute AS red ON l.attribute_id = red.attribute_id
JOIN attribute AS big ON l.attribute_id = big.attribute_id

Regards,
Bill K.
 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Old 07-01-2007, 9:34 PM   #4
strawberry
 
strawberry's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default nested selects on mysql_4.0

Actually, I think my solution might be flawed. What happens when two or
more attributes are assigned but only one of them matches the criteria?

A useful tip: When both Bill and I post on the same topic, ignore mine
and go with Bill's. Well, it works for me :-)

 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Old 07-01-2007, 9:34 PM   #5
Bill Karwin
 
Bill Karwin's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default nested selects on mysql_4.0

Bill Karwin wrote:
> SELECT w.*
> FROM widget AS w
> JOIN attribute_list AS l ON w.widget_id = l.widget_id
> JOIN attribute AS red ON l.attribute_id = red.attribute_id
> JOIN attribute AS big ON l.attribute_id = big.attribute_id


I forgot to add a clause to ensure that the red and bug correlation
names are the right attributes:

WHERE red.attribute_name = 'red' AND big.attribute_name = 'big'

Regards,
Bill K.
 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Old 07-01-2007, 9:34 PM   #6
Bill Karwin
 
Bill Karwin's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default nested selects on mysql_4.0

strawberry wrote:
> Actually, I think my solution might be flawed. What happens when two or
> more attributes are assigned but only one of them matches the criteria?
>
> A useful tip: When both Bill and I post on the same topic, ignore mine
> and go with Bill's. Well, it works for me :-)


Wow, thanks! But I made a mistake in mine anyway! :-)

Anyway, your solution looked fine to me, if one can assume a primary key
defined over (widget_id, attribute_id) in the intersection table.

Regards,
Bill K.
 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Old 07-01-2007, 9:34 PM   #7
Sandy.Pittendrigh@gmail.com
 
Sandy.Pittendrigh@gmail.com's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default nested selects on mysql_4.0

Thanks guys. I'm going to have to try this and think about
it in another night or two. I have to leave town
for a few days tomarrow moroning (with custom spelling
particularly appropriate for monday mornings).

But this is helpful. These cascading aliases remind me
a little of Duff's device, which I still haven't got my head
completely around, now some ten years later. If I try it
and it works, I don't necessarily have to know why.

 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Old 07-01-2007, 9:34 PM   #8
roch77@gmail.com
 
roch77@gmail.com's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default nested selects on mysql_4.0

I think you would need a self join on attrib_list. So you would have
to do this :

select a.widget_id
from attrib_list as a
inner join attrib_list as b on a.widget_id=b.widget_id
inner join attrib as c on a.attrib_id=c.attrib_id
inner join attrib as d on a.attrib_id=d.attrib_id
where c.attrib_name='red'
and d.attrib_name='big';

 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Old 07-01-2007, 9:35 PM   #9
Sandy.Pittendrigh@gmail.com
 
Sandy.Pittendrigh@gmail.com's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default nested selects on mysql_4.0

create table widget
(
widget_id int not null primary key auto_increment,
name varchar(48) not null
);

create table attribute
(
attribute_id int not null primary key auto_increment,
attribute varchar(60) not null
);

create table attribute_list
(
widget_id int not null references widget(id),
attribute_id int not null references attribute(attribute_id)
);

insert into widget (widget_id,name) values(null,'bigred');
insert into widget (widget_id,name) values(null,'smallgreen');
insert into attribute (attribute_id,attribute) values(null,'red');
insert into attribute (attribute_id,attribute) values(null,'big');
insert into attribute (attribute_id,attribute) values(null,'green');
insert into attribute (attribute_id,attribute) values(null,'small');
insert into attribute_list (widget_id, attribute_id) values(1,1);
insert into attribute_list (widget_id, attribute_id) values(1,2);
insert into attribute_list (widget_id, attribute_id) values(2,3);
insert into attribute_list (widget_id, attribute_id) values(3,4);

mysql> select * from widget;
+-----------+------------+
| widget_id | name |
+-----------+------------+
| 1 | bigred |
| 2 | smallgreen |
+-----------+------------+

mysql> select * from attribute;
+--------------+-----------+
| attribute_id | attribute |
+--------------+-----------+
| 1 | red |
| 2 | big |
| 3 | green |
| 4 | small |
+--------------+-----------+

mysql> select * from attribute_list;
+-----------+--------------+
| widget_id | attribute_id |
+-----------+--------------+
| 1 | 1 |
| 1 | 2 |
| 2 | 3 |
| 3 | 4 |
+-----------+--------------+

TEST1
mysql> SELECT w.*
-> FROM widget AS w
-> JOIN attribute_list AS l ON w.widget_id = l.widget_id
-> JOIN attribute AS red ON l.attribute_id = red.attribute_id
-> JOIN attribute AS big ON l.attribute_id = big.attribute_id;
+-----------+------------+
| widget_id | name |
+-----------+------------+
| 1 | bigred |
| 1 | bigred |
| 2 | smallgreen |
+-----------+------------+

TEST2
mysql> SELECT w.*
-> FROM widget AS w
-> JOIN attribute_list AS l ON w.widget_id = l.widget_id
-> JOIN attribute AS red ON l.attribute_id = red.attribute_id
-> JOIN attribute AS big ON l.attribute_id = big.attribute_id
-> where red.attribute = 'red'
-> and big.attribute = 'big';
Empty set (0.00 sec)

TEST3
mysql> SELECT w.*
-> FROM widget AS w
-> JOIN attribute_list AS list ON w.widget_id = list.widget_id
-> JOIN attribute AS red ON list.attribute_id = red.attribute_id
-> JOIN attribute AS big ON list.attribute_id = big.attribute_id
-> where red.attribute = 'red'
-> and big.attribute = 'big';
Empty set (0.00 sec)

 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Old 07-01-2007, 9:35 PM   #10
Sandy.Pittendrigh@gmail.com
 
Sandy.Pittendrigh@gmail.com's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default nested selects on mysql_4.0

OK: here's what seems to work

SELECT w.*
FROM widget AS w
JOIN attribute_list AS alist1 ON w.widget_id = alist1.widget_id
JOIN attribute_list AS alist2 ON w.widget_id = alist2.widget_id
JOIN attribute_list AS alist3 ON w.widget_id = alist3.widget_id

JOIN attribute AS red ON alist1.attribute_id = red.attribute_id
JOIN attribute AS big ON alist2.attribute_id = big.attribute_id
JOIN attribute AS magenta ON alist3.attribute_id = magenta.attribute_id

where red.attribute = 'red' and big.attribute = 'big' and
magenta.attribute='magenta'

 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Featured Websites
Free Space
Free Space
Free Space Free Space
Closed Thread
Tags: , ,




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Possible to have nested <div> tags! Jukka K. Korpela HTML 6 07-01-2007 1:23 PM
Possible to have nested <div> tags! Tim Radford HTML 0 07-01-2007 1:22 PM
Nested HTML Documents Christopher R. Peterson HTML 2 07-01-2007 1:22 PM
how to find nested dupes Jaak Database 3 06-10-2007 12:18 AM
DB_NestedSet, where did my nested set go? Daniel Khan Pear 0 05-20-2007 6:34 PM


Featured Websites




All times are GMT +1. The time now is 12:22 AM.


Powered by: vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO 3.0.0
Cheap Computers
MK PitStop Copyright 2005 - 2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98