473,394 Members | 1,203 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,394 software developers and data experts.

Finding lookup fields in MySQL

I've got a lookup table for a lookup table and can't work out the SQL to
do a search. Can anyone help?

As an example, consider the following:

Table 1 - books:
fields: book_id, price, title, author
data:
1, 2.99, 'Life', 'A. Slug'
2, 4.99, 'The Universe', 'A. Planet'

Table 2 - book_fields:
fields: field_id, field_name
data:
1, 'cover_colour'
2, 'cover_type'

Table 3 - book_field_lookup:
data: book_id, field_id, field_value
1, 1, 'blue'
1, 2, 'hardback'
2, 1, 'blue'
2, 2, 'softback'
How do I do all hardback books that are blue? I'm sure that the answer
is simple, but have a block today :(

--
Rob Allen
Jul 16 '05 #1
4 6374
In message <Mz**************@rob.mustardhouse.com>, Rob Allen
<ro***@the-allens.net> writes
How do I do all hardback books that are blue? I'm sure that the answer
is simple, but have a block today :(


Umm.. That should be "How do I find all the hardback books that are
blue?"

--
Rob Allen
Jul 16 '05 #2
so do you need the sql?

something like

select * from table1, table3 where table1.bood_id = table3.book_id and
table3.field_value like "%hardback%';
"Rob Allen" <ro***@the-allens.net> wrote in message
news:Mz**************@rob.mustardhouse.com...
I've got a lookup table for a lookup table and can't work out the SQL to
do a search. Can anyone help?

As an example, consider the following:

Table 1 - books:
fields: book_id, price, title, author
data:
1, 2.99, 'Life', 'A. Slug'
2, 4.99, 'The Universe', 'A. Planet'

Table 2 - book_fields:
fields: field_id, field_name
data:
1, 'cover_colour'
2, 'cover_type'

Table 3 - book_field_lookup:
data: book_id, field_id, field_value
1, 1, 'blue'
1, 2, 'hardback'
2, 1, 'blue'
2, 2, 'softback'
How do I do all hardback books that are blue? I'm sure that the answer
is simple, but have a block today :(

--
Rob Allen

Jul 16 '05 #3
On Mon, 11 Aug 2003 12:35:55 +0000 (UTC), Rob Allen <ro***@the-allens.net>
wrote:
I've got a lookup table for a lookup table and can't work out the SQL to
do a search. Can anyone help?

As an example, consider the following:

Table 1 - books:
fields: book_id, price, title, author
data:
1, 2.99, 'Life', 'A. Slug'
2, 4.99, 'The Universe', 'A. Planet'

Table 2 - book_fields:
fields: field_id, field_name
data:
1, 'cover_colour'
2, 'cover_type'

Table 3 - book_field_lookup:
data: book_id, field_id, field_value
1, 1, 'blue'
1, 2, 'hardback'
2, 1, 'blue'
2, 2, 'softback'
How do I do all hardback books that are blue? I'm sure that the answer
is simple, but have a block today :(


Join to your books table with your field tables twice, once for type, once for
colour, something like:

SELECT b.book_id
FROM books b,
book_fields bf_colour, book_field_lookup bfl_colour,
book_fields bf_type, book_field_lookup bfl_type
WHERE bf_colour.field_name = 'cover_colour'
AND bf_colour.field_id = bfl_colour.field_id
AND bfl_colour.field_value = 'blue'
AND bfl_colour.book_id = b.book_id
AND bf_type.field_name = 'cover_type'
AND bf_type.field_id = bfl_type.field_id
AND bfl_type.field_value = 'hardback'
AND bfl_type.book_id = b.book_id

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 16 '05 #4
In message <28********************************@4ax.com>, Andy Hassall
<an**@andyh.co.uk> writes

Join to your books table with your field tables twice, once for type,
once for colour, something like:

Ah... You can multiply join the same table! So for N search criteria, I
need to join N times...

Thanks !

--
Rob Allen
Jul 16 '05 #5

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

Similar topics

2
by: Eric Linders | last post by:
Hi everyone. :-) Our site gets a ton of traffic on our contact forms, which collect the standard information (name, address, city, state, zip, home phone, etc.) The form validation is done with...
4
by: Aaron W. West | last post by:
Timings... sometimes there are almost too many ways to do the same thing. The only significant findings I see from all the below timings is: 1) Integer math is generally fastest, naturally....
3
by: my-wings | last post by:
I've been reading about how evil Lookup fields in tables are, but I've got to be missing something really basic. I know this subject has been covered before, because I've just spent an hour or two...
1
by: Zachary Turner | last post by:
I want to make a Lookup Field based on another Lookup field. In other words, I have this table A with two fields: ID and Name, where ID is an Autonumber and Name is a friendly name. Then I have a...
3
by: google | last post by:
I have a database with four table. In one of the tables, I use about five lookup fields to get populate their dropdown list. I have read that lookup fields are really bad and may cause problems...
4
by: jon f kaminsky | last post by:
Hi- I've seen this problem discussed a jillion times but I cannot seem to implement any advice that makes it work. I am porting a large project from VB6 to .NET. The issue is using the combo box...
2
by: Lance Williams | last post by:
How can you programatically tell if a field in a table is a lookup field? I am looping through the fields collection and I want to see if any fields are lookup fields. Is there a property, or...
3
by: Markw | last post by:
I think this was recently asked but I was a little lost on the example that was used so I'm reasking it with an example I can understand. Also forgive me for such a basic question but I really am...
7
by: samdev | last post by:
I have set up a table with a few fields that are set to required....all work fine except the fields that are Lookup from another table. What am I forgetting to set? Thanks!!
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.