473,769 Members | 6,160 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_look up:
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 6410
In message <Mz************ **@rob.mustardh ouse.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_va lue like "%hardback% ';
"Rob Allen" <ro***@the-allens.net> wrote in message
news:Mz******** ******@rob.must ardhouse.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_look up:
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_look up:
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_look up bfl_colour,
book_fields bf_type, book_field_look up bfl_type
WHERE bf_colour.field _name = 'cover_colour'
AND bf_colour.field _id = bfl_colour.fiel d_id
AND bfl_colour.fiel d_value = 'blue'
AND bfl_colour.book _id = b.book_id
AND bf_type.field_n ame = 'cover_type'
AND bf_type.field_i d = bfl_type.field_ id
AND bfl_type.field_ value = 'hardback'
AND bfl_type.book_i d = 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
3252
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 PHP. To eliminate (or at least deter) people wasting time submitting phony information, we can compare the state to the zip code (one DB table) and the zip code to the area code (another table in same DB).
4
2051
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. Bigint math isn't much slower, for integers that all fit within an integer. 2) Converting float to varchar is relatively slow, and should be avoided if possible. Converting from integer to varchar or varchar to int is several times faster.
3
2922
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 reading about it on google, but there is something I still don't understand, and I'm hoping someone will be willing to explain it to me in small words. Let's say I have a table for addresses, and it includes a field for state. What I would...
1
3363
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 table B which links to A via a Lookup Field so that I can select the friendly name from combo box. Now, I want to have a table C which links to table B via a lookup field, and only displays the items which are selected in some combo box of some...
3
10665
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 that are hard to find. The main problem I am having right now is that I have a report that is sorted by one of these lookup fields and it only displays the record's ID number. When I add the source table to the query it makes several records...
4
4620
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 bound to a table as a lookup, drawing values from another table to populate the available selections. This all worked fine in VB6. I have distilled the problem down to a simple form drawing data from the Northwind database for a representative...
2
2397
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 something taht will tell me if the field is a lookup field. I need to do this at the table level and not at the form level because if a field is a lookup field then I do certain things in the code. BTW, lookup fields suck.
3
5330
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 new to Mysql and PHP. In the book "Build Your Own Database Driven Website Using PHP & Mysql" by Kevin Yank, they give an example for when using a many to many relationship that it is best to use a lookup table. The example they show of how it...
7
2588
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
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10211
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9994
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9863
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5299
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3959
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3562
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.